In this article we will see how to use Validator Framework in Struts. These validations include generic validation (required), mask valdiation, email and date validation.
First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here
Add the following dependencies in pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-core</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> </dependencies>
Do not forget to include maven dependencies in Eclipse IDE as shown in following link.
1. Create DynaActionForm
First we create an entry for DynaActionForm in struts-config.xml
LoginForm.java
<form-beans> <form-bean name="UserForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="userName" type="java.lang.String"/> <form-property name="email" type="java.lang.String" /> <form-property name="dob" type="java.lang.String" /> </form-bean> </form-beans>
2. Action Class (Controller)
Let us create an Action class that will handle the request.
UserAction.java
package com.kruders.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorForm; public class UserAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaValidatorForm loginForm = (DynaValidatorForm) form; if(loginForm.get("userName").equals("test") && loginForm.get("password").equals("test")){ return mapping.findForward("success"); } else { return mapping.findForward("fail"); } } }
3. Properties File
Create ApplicationResources.properties and write the following text.
errors.header=<div style="color:red"> errors.required={0} is required. errors.email={0} is an invalid e-mail address. errors.date={0} is invalid date. UserForm.userName=Name UserForm.email=Email UserForm.dob = Date Of Birth UserForm.userName.invalid = {0} should contain only alphabets.
4. Enable Validator Plug-in
Write the following code in struts-config.xml to enable the Validator Plug-in.
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in>
5. Validation Rules
The following validations are defined in the validation.xml file.
<form name="UserForm"> <field property="userName" depends="required,mask"> <msg name="mask" key="UserForm.userName.invalid" /> <arg key="UserForm.userName" /> <var> <var-name>mask</var-name> <var-value>^[a-zA-Z]*$</var-value> </var> </field> <field property="email" depends="required,email"> <arg key="UserForm.email" /> </field> <field property="dob" depends="required,date"> <arg key="UserForm.dob"/> <var> <var-name>datePattern</var-name> <var-value>MM-dd-yy</var-value> </var> </field> </form>
The required rule ensure that the value cannot be empty, mask rule is used to restrict the user from entering invalid data. The date rule checks whether the date is valid or not. The email rule is used for email validation.
6. View
Create following JSP files, login.jsp, success.jsp and fail.jsp with the following content in your WebContent folder.
login.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head> <title>Struts Validator Framework - Login Page</title> </head> <body> <html:form action="/login" > <table> <tr> <td> User Name : </td> <td> <html:text name="UserForm" property="userName"/> </td> <td> <html:errors property="userName" /> </td> </tr> <tr> <td> Email : </td> <td> <html:text name="UserForm" property="email" /> </td> <td> <html:errors property="email" /> </td> </tr> <tr> <td> Date of Birth : </td> <td> <html:text name="UserForm" property="dob" /> </td> <td> <html:errors property="dob" /> </td> </tr> <tr> <td></td> <td> <html:submit value="Submit" /> </td> <td></td> </tr> </table> </html:form> </body> </html>
The view login.jsp is to provide user to enter user details and submitting for Login.
success.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Success Page</title> </head> <body> Login Success. Welcome <bean:write name="LoginForm" property="userName"></bean:write> </body> </html>
The view success.jsp is to show success message.
fail.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Failure Page</title> </head> <body> Invalid User Name or Password </body> </html>
The view fail.jsp is to show failure message.
7. Struts Config
Create a struts-config.xml file in WEB-INF folder.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="UserForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="userName" type="java.lang.String"/> <form-property name="email" type="java.lang.String" /> <form-property name="dob" type="java.lang.String" /> </form-bean> </form-beans> <global-forwards> <forward name="login" path="/login.do"/> </global-forwards> <action-mappings> <action input="/login.jsp" name="UserForm" path="/login" validate="true" scope="request" type="com.kruders.action.LoginAction"> <forward name="success" path="/success.jsp" /> <forward name="fail" path="/fail.jsp" /> </action> </action-mappings> <message-resources parameter="ApplicationResources" /> <!-- Validator plugin --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> </struts-config>
8. Deployment Descriptor
Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do
Add the following configuration information in the web.xml file
<welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
9. Run Project
Now when you run the project, following screen will be displayed as shown in Figure 42.1
Figure 42.1
When you click Submit button with with all field empty, following screen will be displayed as shown in Figure 42.2
Figure 42.2
If you enter invalid value, following screen will be displayed as shown in Figure 42.3
Figure 42.3
The folder structure of the example is shown below in Figure 42.4
Figure 42.4
One Response to Struts Validator Framework