0

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>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83
  • Do you have already tried the pretty new struts2-jsr303 plugin? https://github.com/umeshawasthi/jsr303-validator-plugin – Johannes Feb 03 '14 at 16:41
  • @jogep, can you see my EDIT? – Volodymyr Levytskyi Feb 03 '14 at 17:13
  • @jogep. This plugin works if I not use struts2-fullhibernatecore-plugin. But when I use it then validating errors are not shown on my jsp page, but it still prevents hibernate to insert new user. – Volodymyr Levytskyi Feb 03 '14 at 18:20
  • @VolodymyrLevytskyi: Can you have any sample application showing issue you have mentioned? I am wondering what is stopping plugin not to show errors on UI..Can you put some sample app on github or anywhere else if possible. – Umesh Awasthi Apr 07 '14 at 16:23

0 Answers0