2013-12-10 85 views
2

使用驗證api時出現錯誤。我驗證了register.jsp中的輸入 我已經包含了所需的罐子。 驗證-API-1.1.0.Final.jar 休眠驗證器-5.0.1.Final.jarSpring Form驗證(JSR-303)

register.jsp

<h4>Register</h4> 
<form:form method="post" action="register" modelAttribute="user"> 
    <form:label path="userName">UserName</form:label> 
    <form:input path="userName"/> 
    <form:errors path="userName"/><br> 
    <form:label path="password">Password</form:label> 
    <form:password path="password"/> <br> 
    <input type="submit" value="Register"> 
</form:form> 

User.java

@Entity 
@Table(name="usertable") 
public class User { 
    @Id 
    @Column(name="username") 
    @Size(min=6,max=10,message="Error Message") 
    private String userName; 
    @Column(name="password") 
    private String password; 

當我輸入小於6的用戶名,result.hasErrors()爲false,但它應該是true。 RegisterController.java

@Controller 
@RequestMapping(value="register") 
public class RegisterController { 
    @Autowired 
    private RegisterService registerService; 
    @RequestMapping(method=RequestMethod.POST) 
    public ModelAndView register(@Valid User user,BindingResult result){ 
     System.out.println("hi"); 
     if(result.hasErrors()){ 
      return new ModelAndView("index"); 
     } 
     else{ 
      if(registerService.register(user)) 
       return new ModelAndView("success"); 
      return new ModelAndView("failure"); 
     } 
    } 
    public RegisterService getRegisterService() { 
     return registerService; 
    } 
    public void setRegisterService(RegisterService registerService) { 
     this.registerService = registerService; 
    } 
} 

RegisterService.java

@Service 
public class RegisterService { 
    @Autowired 
    private RegisterDao registerDao; 
    @Transactional 
    public boolean register(User user){ 
     registerDao.register(user); 
     return true; 
    } 
    public RegisterDao getRegisterDao() { 
     return registerDao; 
    } 
    public void setRegisterDao(RegisterDao registerDao) { 
     this.registerDao = registerDao; 
    } 
} 

RegisterDao.java

@Repository 
public class RegisterDao { 
    @Autowired 
    private SessionFactory sessionFactory; 
    public boolean register(User user){ 
     this.sessionFactory.getCurrentSession().save(user); 
     return true; 
    } 
    public SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
    public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 
} 

正在此錯誤

SEVERE: Servlet.service() for servlet [spring] in context with path [/Annotated] threw exception [Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.labs.domain.User] during persist time for groups [javax.validation.groups.Default, ] 
List of constraint violations:[ 
    ConstraintViolationImpl{interpolatedMessage='Error Message', propertyPath=userName, rootBeanClass=class com.labs.domain.User, messageTemplate='Error Message'} 
]] with root cause 
javax.validation.ConstraintViolationException: Validation failed for classes [com.labs.domain.User] during persist time for groups [javax.validation.groups.Default, ] 
List of constraint violations:[ 
    ConstraintViolationImpl{interpolatedMessage='Error Message', propertyPath=userName, rootBeanClass=class com.labs.domain.User, messageTemplate='Error Message'} 
] 

有人能告訴我,我在哪裏去錯了嗎?

回答

0

嘗試第二所需的註釋添加到控制器RequestMapping:

@RequestMapping(method=RequestMethod.POST) 
public ModelAndView register(@Valid @ModelAttribute User user, BindingResult result) { 
    ... 
} 

此外,有沒有在其中加載頁面時開始,在提交前用適當的用戶養活模型GET請求?

+0

是Jeyp,我將模型addribute傳遞給register.jsp。 –

+0

在添加@ModelAttribute之後也不行... –

+0

請注意,在默認配置中,添加'@ ModelAttribute'或將它關閉是等同的。 –

0

閱讀春天驗證文件後,我才知道,我必須在我的春天配置中添加<mvc:annotation-driven/>