In this example, you will see how to implement client side validation in
struts2 application. We
can easily implement client side validation in struts2.
If we adds validate="true" in struts2 form
tags, than it will
works as client side validation in struts.
Note:- if you wants to see struts generated JavaScript then display view source of your page.
index.jsp
<%@ page language="java" contentType="text/html;charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%><%@ taglib uri="/struts-tags" prefix="s" %>< html>< head><title>Index Page</title></head>< body><h3>Struts2_Client_Side_Validation_Example</h3>< s:a href="registrationForm.action"><FONT color="green" >Go To Login Page..</FONT> </s:a> </body> </html> |
RegistrationForm.jsp
<%@ page language="java" contentType="text/html;charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s" %> < html>< head><title>Registration Form</title></head><s:head/>< body><h3 >Struts2_Client_Side_Validation_Example</h3><hr>< font style="color: green;"> Registration Form</font>< s:form action="registrationProcess.action"name="registration" method="post"> <s:textfield key="firstname" /> <s:password key="password"/> <s:submit value="Registration"/></s:form> </body></html> |
RegistrationAction.java (Action Class)
package roseindia.action;import roseindia.Model.RegistrationModel;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class RegistrationAction extends ActionSupport implementsModelDriven<Object> { RegistrationModel obRegModel; public String execute() { return SUCCESS; }@Override public Object getModel() { obRegModel = new RegistrationModel(); return obRegModel; }} |
RegistrationAction-validation.xml
<! DOCTYPE validators PUBLIC"-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >< validators>< field name="firstname">< field-validator type="requiredstring"> <message key="requiredstring"/></ field-validator> </field>< field name="password">< field-validator type="requiredstring"> <message key="requiredstring"/></ field-validator></ field>< field name="password">< field-validator type="stringlength">< param name="minLength">5</param>< param name="maxLength">15</param>< param name="trim">true</param>< message >Please enter Min %{minLength} character or Max %{maxLength}Character </message> </ field-validator></ field></validators> |
package.properties
requiredstring = ${getText(fieldName)} is required.firstname = Student Namepassword= Password |
RegistrationModel.java (Model)
package roseindia.Model;import java.io.Serializable; public class RegistrationModel implements Serializable{ private String firstname; private String password; public String getFirstname() { return firstname; }public void setFirstname(String firstname) { this.firstname = firstname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
struts.xml
<? xml version="1.0" encoding="UTF-8"?><! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">< struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /><package name="roseindia" namespace="/" extends="struts-default"> <action name="registrationForm"> <result>Registration/RegistrationForm.jsp</result> </action> <action name="registrationProcess" class="roseindia.action.RegistrationAction"> <result name="input">Registration/RegistrationForm.jsp</result> <result name="error">Registration/RegistrationForm.jsp</result> <result>Registration/RegistrationSuccess.jsp</result> </action></package></ struts> |
RegistrationSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s" %><! DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>Insert title here</title></head> <body><h2 style="color: green">Hi, < s:property value="firstname"/>your welcome. </h2>< h3 style="color: gray;">Login success.</h3></body></ html> |
Struts generated JavaScript code. (Registrartion.jsp)
<html> <head> <title>Registration Form</title></head> <link rel="stylesheet" href="/Struts2_Client_Side_Validation_Example/struts /xhtml/styles.css" type="text/css"/> <script src="/Struts2_Client_Side_Validation_Example/struts/utils.js" type="text/javascript"></script> <body> <h3 >Struts2_Client_Side_Validation_Example</h3> <hr> <font style="color: green;"> Registration Form</font> <form id="registrationProcess" name="registration" action= "registrationProcess.action" method="post"> <table class="wwFormTable"> <tr errorFor="registrationProcess_firstname"> <td align="center" valign="top" colspan="2"><span class="errorMessage"> Student Name is required.</span></td></tr> <tr> <td class="tdLabel"><label for="registrationProcess_firstname" class= "errorLabel">Student Name:</label></td> <td><input type="text" name="firstname" value="" id= "registrationProcess_firstname"/></td></tr> <tr errorFor="registrationProcess_password"> <td align="center" valign="top" colspan="2"><span class= "errorMessage">Password is required.</span></td></tr> <tr> <td class="tdLabel"><label for="registrationProcess_password" class="errorLabel">Password:</label></td> <td><input type="password" name="password" id="registrationProcess_password"/></td></tr> <tr><td colspan="2"><div align="right"><input type= "submit" id="registrationProcess_0" value="Registration"/> </div></td></tr></table></form></body></html> |
Output
Advertisements
Ads
Ads