如何在struts2中使用@Valid注释触发validation?

我无法在hibernate,struts2 webapp中使@javax.validation.Valid注释工作。

这是我的简单实体与约束:

 import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @Entity @Table(name = "user") public class User implements Serializable { @Id @GeneratedValue @Column(name = "user_id") private Long id; @NotEmpty @Length(max = 50, min = 3, message = "Name should be not less 3 and not more 50") @Column(name = "name", nullable = false) private String name; @Length(min = 6, max = 10) @Column(name = "password") private String password; @NotEmpty(message = "Please select a gender") @Column(name = "gender") private String gender; @NotEmpty(message = "Please select a country") @Column(name = "country") private String country; @Length(max = 100) @Column(name = "aboutYou") private String aboutYou; @Column(name = "mailinglist") private Boolean mailingList; 

这是我使用@Valid注释的UserAction类:

 public class UserAction extends ActionSupport implements ModelDriven { @Valid private User user = new User(); private UserDao dao = new UserDao(); private List users = new ArrayList(); public String addUser() { dao.addUser(user); users = dao.listUsers(); return SUCCESS; } public List getUsers() { return users; } public User getModel() { return user; } } 

这是我的jsp,它应该在hibernate约束失败时显示错误消息。

 

Add user:

当我提交空表单时, 没有hibernate约束工作 ,新用户被添加到数据库,当然在jsp页面上没有显示错误。

你不知道什么是错的吗?

编辑 。 在尝试使用推荐的插件后,我得到了NullPointer,因为hibernate Session没有在@SesionTarget注释的帮助下注入。 这是我的struts.xml,它扩展了jsr303而不是hibernate-default。

    /index.jsp /index.jsp   /index.jsp /index.jsp