【问题标题】:Spring Form Validation (JSR-303)Spring 表单验证 (JSR-303)
【发布时间】:2013-12-27 21:09:40
【问题描述】:

使用验证 api 时出错。正在验证 register.jsp 中的输入 我已经包括了所需的罐子。 验证-api-1.1.0.Final.jar hibernate-validator-5.0.1.Final.jar

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>

用户.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'}
]

谁能告诉我哪里出错了?

【问题讨论】:

    标签: hibernate spring-mvc spring-form


    【解决方案1】:

    尝试将第二个需要的注解添加到Controller RequestMapping:

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

    另外,是否有一个 GET 请求,您在最初加载页面时向模型提供适当的用户,然后再提交?

    【讨论】:

    • 是的 Jeyp,我将模型添加到 register.jsp。
    • 请注意,在默认配置中,添加 @ModelAttribute 或将其关闭是等效的。
    【解决方案2】:

    在阅读了 spring 验证文档后,我知道了,我必须在我的 spring 配置中添加&lt;mvc:annotation-driven/&gt;

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2013-11-23
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2012-11-24
      • 2011-05-10
      • 2018-12-15
      相关资源
      最近更新 更多