我在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;
}
您不能通过使用控制器直接调用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/
