How to trigger validation with @Valid annotation in struts2?

怎甘沉沦 提交于 2019-12-10 12:27:52

问题


I cannot make @javax.validation.Valid annotation work in hibernate,struts2 webapp.

Here is my simple Entity with constraints:

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;

Here is my UserAction class that uses @Valid annotation:

public class UserAction extends ActionSupport implements ModelDriven<User> {

    @Valid
    private User user = new User();
    private UserDao dao = new UserDao();
    private List<User> users = new ArrayList<User>();

    public String addUser() {
        dao.addUser(user);
        users = dao.listUsers();
        return SUCCESS;
    }

    public List<User> getUsers() {
        return users;
    }

    public User getModel() {
        return user;
    }
}

Here is my jsp which should display error messages when hibernate constraints fail.

<h4>Add user:</h4>
    <s:actionerror/>
    <s:form action="addUser">
        <s:textfield key="name" />
        <s:password key="password" />
        <s:radio list="{'Male','Female'}" key="gender" />
        <s:select list="{'India','USA','Ukraine'}" headerKey=""
            headerValue="Select" key="country" />
        <s:textarea key="aboutYou" cols="20" rows="10" />
        <s:checkbox key="mailingList" value="true" />
        <s:submit value="Post" />
    </s:form>

When I submit empty form then no hibernate constraint works and new user is added to database and of course no error is displayed on jsp page.

Don't you know what's wrong?

EDIT. After trying to use recommended plugin I got NullPointer because hibernate Session wasn't injected with help of @SesionTarget annotation. Here is my struts.xml that extends jsr303 instead of hibernate-default.

<package name="default" extends="jsr303">
        <default-interceptor-ref name="jsr303ValidationStack"/>

        <action name="index" class="com.shb.action.UserAction" method="execute">
            <result name="success">/index.jsp</result>
            <result name="input">/index.jsp</result>
        </action>

        <action name="addUser" class="com.shb.action.UserAction" method="addUser">
            <result name="success">/index.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>

来源:https://stackoverflow.com/questions/21532136/how-to-trigger-validation-with-valid-annotation-in-struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!