Java – Spring MVC 3 validation with annotations – not showing form:errors

javaspringspring-mvc

I've been stuck with this about one hour, this is not the first time i am doing this but I cant figure it out.

It should show me the form errors, instead it throws this exception.

LE: i managed to solve the exception in the meantime, but i still cant see the errors. they are not rendered

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'category' on field 'name': rejected value [jk]; codes [Range.category.name,Range.name,Range.java.lang.String,Range]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [category.name,name]; arguments []; default message [name],25,3]; default message [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;
}

Best Answer

There seems to be a dependency on the order in which the parameters must be listed in the mapped controller method.
For example:

public String addCategory(@Valid CategoryAdd category,BindingResult result, 
Map map)

Will work, whereas:

public String addCategory(@Valid CategoryAdd category, Map map, 
BindingResult result)

Will not. I don't full understand why this is, especially as it compiles without any issues.

Related Topic