如何将 Bootstrap 弹窗映射到 Spring MVC 控制器

6
我有一个Bootstrap Modal中的表单,我希望我的Spring MVC控制器监听它。我的问题是,由于模态框在当前页面内部,所以它不会生成href,因此我无法仅将模态框映射到我的Spring MVC控制器。
我需要这样做,因为我想从bindingresult对象中显示错误信息。我该怎么办?
这是我的模态框:http://www.bootply.com/zerZIYpNAF 假设它位于index.jsp中,所以虚构的路径可能是/index#myModal.jsp或类似的东西。
@RequestMapping(value="/send", method = RequestMethod.GET)
public String get(Dummybean bean){
    return "??"; //index#myModal
}

@RequestMapping(value="/send", method = RequestMethod.POST)
public String post(@Valid @ModelAttribute("dummy") DummyBean bean, BindingResult bindingResult){

if(bindingResult.hasErrors()){
        return "??"; //index#myModal
    }
//do something
}



public class DummyBean{


@NotNull
private String name;

public String getName() {
    return username;
}

public void setName(String name) {
    this.name = name;
}
2个回答

0

你无法直接使用控制器调用bootstrap模态框弹出。因此,你将不能绑定Spring表单。但是你可以使用Ajax来实现它。你必须像普通的HTML表单一样使用表单,而不使用Spring标签。

function searchAjax() {
    var data = {}
    data["query"] = $("#query").val();

    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : "${home}search/api/getSearchResult",
        data : JSON.stringify(data),
        dataType : 'json',
        timeout : 100000,
        success : function(data) {
            console.log("SUCCESS: ", data);
            display(data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });
}

这是一个 AJAX 的示例,让您了解一下。您需要使用 HttpServletRequest 从控制器端检索数据。上面的示例来自 http://www.mkyong.com/spring-mvc/spring-4-mvc-ajax-hello-world-example/


-1

1) 创建新的函数专门用于验证

2) 创建JS函数,优先使用jQuery并向步骤一中的函数发送Ajax请求。

3) 根据验证状态处理错误或完全发送表单。

请阅读本文,它完全回答了您的问题javacodegeeks.com


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接