我在Bootstrap Modal中有一个表单,我想让我的Spring MVC控制器监听它。我的问题是,模式不会生成href,因为它在当前页面中,所以我不能只在Spring MVC控制器中映射模式。
我需要它,因为我想显示来自bindingresult对象的错误。我该怎么做呢?
这是我的模式:,假设它位于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;
}
发布于 2016-10-14 11:40:10
您不能使用控制器直接调用bootstrap模式来弹出。因为你将不能用Spring绑定form。但是您可以使用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/
发布于 2014-12-01 18:06:09
1)创建仅用于验证的新函数
2)使用首选jquery创建js函数,并向第一步中的函数发送ajax请求。
3)根据验证状态将处理错误或完全发送表单。
请阅读这篇文章,它完全回答了您的问题javacodegeeks.com
https://stackoverflow.com/questions/26677518
复制相似问题