问题
I got a new roo project off the ground successfully, but now I'm having a problem getting validation to work for a login page. It seems that the validator is choking before passing control to my controller. I am never given the opportunity to check the BindingResult. I've examined several similar questions here and on the web, and my code seems to conform with what they are doing.
First the error I get when submitting the form (if i pass validation i get no error message). In this case i didn't meet the minimum length for the password:
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 'loginUser' on field 'password': rejected value [b]; codes [Size.loginUser.password,Size.password,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [loginUser.password,password]; arguments []; default message [password],50,5]; default message [Password must be between 1 and 50 characters long]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
My entity validation is setup like this:
public class LoginUser {
@NotNull
@NotEmpty
private String username;
@NotNull
@NotEmpty(message = "Password must not be blank.")
@Size(min = 5, max = 50, message = "Password must be between 1 "
+ " and 50 characters long")
private String password;
}
Here is my markup:
<form:form method="post" commandName="command">
<form:label path="username">Username: </form:label>
<form:input path="username"/>
<form:errors path="username"/>
<form:label path="password">Password: </form:label>
<form:password path="password"/>
<form:errors path="password"/>
<input type="submit"/>
</form:form>
And the controller:
@ModelAttribute("command")
public LoginUser fbo(){
LoginUser u = new LoginUser();
u.setUserType(UserType.USER);
return u;
}
@RequestMapping(value="/login.htm", method=RequestMethod.POST)
public String doLogin(@Valid LoginUser command,
HttpServletRequest request, BindingResult result
){
if(result.hasErrors()){
return "login";
}
}
And just in case it matters, the generated xml in webmvc-config.xml:
<!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
<context:component-scan base-package="com.tcg.myproject" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<!-- Turns on support for mapping requests to Spring MVC @Controller methods
Also registers default Formatters and Validators for use across all @Controllers -->
<mvc:annotation-driven conversion-service="applicationConversionService"/>
Been pulling my hair out for hours and I can't figure out what it could be. Thanks for reading!
回答1:
In your controller handler method, try moving the BindingResult argument so it is immediately after the command argument. Spring looks for command object parameters and BindingResult parameters to be paired up in handler method signatures.
来源:https://stackoverflow.com/questions/9916623/spring-valid-validator-not-invoked-properly-roo-hibernate