Reputation: 2526
I'm trying to use ModelDriven
approach + validation on the server side using annotations. But it seems like annotations simply don't work. I have a simple User
class with name
, surname
, email
, etc:
@Entity
@Table(name = "USER")
@Validation
public class User {
private Long id;
private String name;
private String gender;
private String country;
private String aboutYou;
private Boolean mailingList;
...
@Column(name = "USER_NAME")
@RequiredStringValidator(message = "User Name field is empty.")
public String getName() {
return name;
}
@Column(name = "USER_GENDER")
@RequiredStringValidator(message = "User Gender field is empty.")
public String getGender() {
return gender;
}
...
}
I use @RequiredStringValidator
for validation on gender
and name
fields.
My UserAction
class that processes requests is:
@Validation
public class UserAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = -6659925652584240539L;
private User user = new User();
private List<User> userList = new ArrayList<User>();
private UserDAO userDAO = new UserDAOImpl();
@VisitorFieldValidator(message = "", appendPrefix = false)
public User getModel() {
return user;
}
/**
* To save or update user.
*
* @return String
*/
@VisitorFieldValidator(message = "", appendPrefix = false)
public String saveOrUpdate() {
userDAO.saveOrUpdateUser(user);
return SUCCESS;
}
/**
* To list all users.
*
* @return String
*/
public String list() {
userList = userDAO.listUser();
return SUCCESS;
}
@VisitorFieldValidator(message = "", appendPrefix = false)
public User getUser() {
return user;
}
...
}
I use @VisitorFieldValidator(message = "", appendPrefix = false)
, because I've read that it is needed in case with ModelDriven
approach.
My struts.xml
is:
<struts>
<package name="default" extends="hibernate-default">
<action name="saveOrUpdateUser" method="saveOrUpdate" class="com.tutorials4u.web.UserAction">
<result name="success" type="redirect">listUser</result>
<result name="input">/register.jsp</result>
</action>
<action name="listUser" method="list" class="com.tutorials4u.web.UserAction">
<result name="success">/register.jsp</result>
<result name="input">/register.jsp</result>
</action>
</struts>
I've read that it's needed to define <result name="input">
for validation.
Here is my register.jsp
in which fields that must be validated are defined:
<s:form action="saveOrUpdateUser" method = "POST" validate="true">
<s:actionerror/>
<s:fielderror />
<s:push value="user">
<s:hidden name="id" />
<s:textfield name="name" label="User Name" />
<s:radio name="gender" label="Gender" list="{'Male','Female'}" />
<s:select name="country" list="{'India','USA','UK'}" headerKey=""
headerValue="Select" label="Select a country" />
<s:textarea name="aboutYou" label="About You" />
<s:checkbox name="mailingList"
label="Would you like to join our mailing list?" />
<s:submit />
</s:push>
</s:form>
My web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<description>struts2</description>
<display-name>struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
And the problem is that, all this doesn't work. I still fighting against this for 4 hours and the only idea in my head tells me that, it is easier to make my_own_bicycle
validation, instead of using such convenient solution for validation.
Upvotes: 0
Views: 1856
Reputation: 1
First of all, the Validation
annotation is deprecated.
Second, field names should exactly map the objects in the action. So, the push
tag is useless, the User
object is allredy pushed to the value stack by the modelDriven
interceptor.
The validate="true"
informs Struts that you are using a client-side javascript validation.
Make sure you have s:head
tag and javascript enabled in JSP.
Upvotes: 0