我一直坚持这个约一个小时,这不是我第一次这样做,但我无法弄明白。
它应该显示表单错误,而不是抛出此异常。
LE:我在此期间设法解决了异常,但我仍然无法看到错误。他们没有呈现
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1错误 字段'name'上对象'category'中的字段错误:被拒绝的值[jk];代码[Range.category.name,Range.name,Range.java.lang.String,Range]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [category.name,name];参数[];默认消息[name],25,3];默认消息[gfhghj] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:311) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:323) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
@Controller
@RequestMapping("/category")
public class CategoryController {
@Autowired
StockGateway stockGateway;
@RequestMapping(value = "add", method = RequestMethod.POST)
public String addCategory(@Valid @ModelAttribute("category") CategoryAdd category, ModelMap model,
BindingResult binding) {
if (binding.hasErrors()) {
return "category.add";
}
CategoryDTO dto = Converter.toCategory(category);
try {
stockGateway.createCategory(dto);
} catch (StockGatewayException e) {
e.printStackTrace();
}
return "redirect:/category/add";
}
@RequestMapping(value = "add", method = RequestMethod.GET)
public String initView(ModelMap model) {
model.put("category", new CategoryAdd());
return "category.add";
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:spring="http://www.springframework.org/tags"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:security = "http://www.springframework.org/security/tags"
xmlns:util="urn:jsptagdir:/WEB-INF/tags/util"
xmlns:form="http://www.springframework.org/tags/form"
version="2.0">
<security:authorize access="hasRole('ROLE_STOCK')" var="isStock">
<form:form modelAttribute="category" method="POST" action="add" >
<table>
<tr>
<td>Name :</td> <td> <form:input path="name" /></td>
</tr>
<tr>
<td></td> <td> <form:errors path="name" cssClass="error" /></td>
</tr>
</table>
<form:button>Add</form:button>
</form:form>
</security:authorize>
public class CategoryAdd {
@Range(min= 3 , max = 25, message="gfhghj")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
答案 0 :(得分:23)
似乎必须依赖于映射控制器方法中必须列出参数的顺序 例如:
public String addCategory(@Valid CategoryAdd category,BindingResult result,
Map map)
会工作,而:
public String addCategory(@Valid CategoryAdd category, Map map,
BindingResult result)
不会。我不完全理解为什么会这样,特别是因为它编译时没有任何问题。
答案 1 :(得分:0)
完整的解决方案是:
public String addCategory(@Valid CategoryAdd category,BindingResult result,
Map map
) {
if (result.hasErrors()) {
map.put("categoryAdd", (CategoryAdd)result.getTarget());
return "category.add";
}
CategoryDTO dto = Converter.toCategory(category);
try {
stockGateway.createCategory(dto);
} catch (StockGatewayException e) {
e.printStackTrace();
}
return "redirect:/category/add";
注意我用Map更改了参数ModelMap,如果结果有错误,我会从结果中放回模型。
现在WTF: BindingResult有一个字段objectName,其值为类的名称。 您必须将modelAttribute / commandName设置为该值。与BindingResult中的objectName相同,否则它不会呈现错误。 所以要匹配它:
<form:form modelAttribute="categoryAdd" commandName="categoryAdd" method="POST" action="add" >
有谁知道如何从BindingResult更改objectName?默认情况下,它将采用类的名称。