Java Spring将丢失BindingResult和don';t向视图发送全局错误
发生的情况:当我在BindingResult上放置一个全局错误(使用Java Spring将丢失BindingResult和don';t向视图发送全局错误,java,spring,error-handling,log4j,jstl,Java,Spring,Error Handling,Log4j,Jstl,发生的情况:当我在BindingResult上放置一个全局错误(使用reject)时,它不会出现在我的视图中 我的控制器: @RequestMapping("register/manageCliente/newcliente.do") public String newClienteDo(@ModelAttribute("formCliente") @Valid Cliente formCliente, BindingResult bResult, Model model) t
reject
)时,它不会出现在我的视图中
我的控制器:
@RequestMapping("register/manageCliente/newcliente.do")
public String newClienteDo(@ModelAttribute("formCliente") @Valid Cliente formCliente, BindingResult bResult, Model model) throws Exception {
if(!bResult.hasErrors()) {
try {
//Do Something
} catch (SQLException e) {
logger.error("No newClienteDo :" + e); //it works, the error appear into my log4j.
bResult.reject(e.getMessage());
}
}
if (bResult.hasErrors() || bResult.hasGlobalErrors()) {
model.addAttribute("msgError",bResult.getAllErrors().toString()); //it works, if I put the model in the view
return "register/manageCliente/newcliente";
}
model.addAttribute("formCliente",formCliente);
return "redirect:/register/manageCliente/listcliente";
}
在我看来,我是这样写的
:
<form:form action="newcliente.do" method="post" modelAttribute="formCliente" commandName="formCliente">
<form:errors></form:errors>
</form:form>
我试过
,但也不起作用
它没有显示任何错误,我在使用log4j时log4j.logger.org.springframework=DEBUG
active。它也没有显示任何错误
谢谢,
编辑
它对视图执行了大约4个请求,一直在清理我的拒绝。我用Post-Redirect-Get尝试了这个解决方案,但也不起作用
编辑:2
由于这是一个紧急问题,我将错误放在
model.addAttribute
上,并在视图中用EL打印错误。但是这是不正确的,所以如果有人有更好的解决方案,我会打开这篇文章。删除Model
参数。您不需要添加formCliente
,因为由于@ModelAttribute
,它已经是模型的一部分。BindingResult
也有一个模型(检查该类上的getModel()
方法)。现在,您的空模型将覆盖来自BindingResult
@M.Deinum的模型。我需要向模型添加其他内容,没有模型参数,我如何才能做到这一点?每个示例:将来我需要在模型中添加一些列表以在我的视图中进行迭代。的可能重复要准备模型,建议使用@modeldattribute
带注释的方法,而不是将所有这些内容放在请求处理方法中。如果您真的需要添加东西,只需执行bResult.getModel()
即可获得模型。它有一个getModel
,它为您提供了一个Map
,这就是模型<代码>模型不过是这个地图的包装。同样,正如您当前的方法中所述,您没有向模型中添加任何尚未添加的内容,您基本上是在复制Spring已经为您做的事情。如前所述,如果需要添加其他内容,只需添加一个带注释的方法。