Example of struts2.2.1 client side validation.
Posted on: February 7, 2011 at 12:00 AM
In this example, you will see how to implement client side validation.

Example of struts2.2.1 client side validation.

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.jspADS_TO_REPLACE_1

<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s" %>ADS_TO_REPLACE_2

<html>

<head><title>Index Page</title></head>

<body><h3>Struts2_Client_Side_Validation_Example</h3>ADS_TO_REPLACE_3

<s:a href="registrationForm.action">

<FONT color="green" >Go To Login Page..</FONT> </s:a>

</body>ADS_TO_REPLACE_4

</html>

RegistrationForm.jsp

<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s" %>ADS_TO_REPLACE_5

<html>

<head><title>Registration Form</title></head><s:head/>

<body><h3 >Struts2_Client_Side_Validation_Example</h3><hr>ADS_TO_REPLACE_6

<font style="color: green;"> Registration Form</font>

<s:form action="registrationProcess.action"

 name="registration" method="post">ADS_TO_REPLACE_7

<s:textfield key="firstname" />

<s:password key="password"/>

<s:submit value="Registration"/></s:form>ADS_TO_REPLACE_8

</body></html>

RegistrationAction.java (Action Class)

package roseindia.action;

import roseindia.Model.RegistrationModel;

import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_9

import com.opensymphony.xwork2.ModelDriven;

public class RegistrationAction extends ActionSupport implements

ModelDriven<Object> {ADS_TO_REPLACE_10

RegistrationModel obRegModel;

public String execute() {

return SUCCESS; }ADS_TO_REPLACE_11

@Override

public Object getModel() {

obRegModel = new RegistrationModel();ADS_TO_REPLACE_12

return obRegModel; }

}

RegistrationAction-validation.xml 

<!DOCTYPE validators PUBLIC

"-//OpenSymphony Group//XWork Validator 1.0.2//EN"ADS_TO_REPLACE_13

"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<field name="firstname">ADS_TO_REPLACE_14

<field-validator type="requiredstring">

<message key="requiredstring"/>

</field-validator> </field>ADS_TO_REPLACE_15

<field name="password">

<field-validator type="requiredstring">

<message key="requiredstring"/>ADS_TO_REPLACE_16

</field-validator>

</field>

<field name="password">ADS_TO_REPLACE_17

<field-validator type="stringlength">

<param name="minLength">5</param>

<param name="maxLength">15</param>ADS_TO_REPLACE_18

<param name="trim">true</param>

<message >Please enter Min %{minLength} character or Max %{maxLength}
  Character
</message>

</field-validator>ADS_TO_REPLACE_19

</field>

</validators>

package.properties

requiredstring = ${getText(fieldName)} is required.

firstname = Student NameADS_TO_REPLACE_20

password= Password

RegistrationModel.java (Model)

package roseindia.Model;

import java.io.Serializable;ADS_TO_REPLACE_21

public class RegistrationModel implements Serializable

{

private String firstname;ADS_TO_REPLACE_22

private String password;

public String getFirstname() {

return firstname; }ADS_TO_REPLACE_23

public void setFirstname(String firstname) {

this.firstname = firstname; }

public String getPassword() {ADS_TO_REPLACE_24

return password; }

public void setPassword(String password) {

this.password = password; }ADS_TO_REPLACE_25

}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLICADS_TO_REPLACE_26

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>ADS_TO_REPLACE_27

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

<package name="roseindia" namespace="/" extends="struts-default">ADS_TO_REPLACE_28

<action name="registrationForm">

<result>Registration/RegistrationForm.jsp</result>

</action>ADS_TO_REPLACE_29

<action name="registrationProcess" class="roseindia.action.RegistrationAction">

<result name="input">Registration/RegistrationForm.jsp</result>

<result name="error">Registration/RegistrationForm.jsp</result>ADS_TO_REPLACE_30

<result>Registration/RegistrationSuccess.jsp</result>

</action></package>

</struts>

RegistrationSuccess.jspADS_TO_REPLACE_31

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s" %>ADS_TO_REPLACE_32

<!DOCTYPE html PUBLIC 

"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>ADS_TO_REPLACE_33

<head><title>Insert title here</title></head>

<body><h2 style="color: green">Hi,

<s:property value="firstname"/>ADS_TO_REPLACE_34

your welcome.</h2>

<h3 style="color: gray;">Login success.</h3></body>

</html>

Struts generated JavaScript code. (Registrartion.jsp)ADS_TO_REPLACE_35

<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

ADS_TO_REPLACE_36

ADS_TO_REPLACE_37

Download Source Code


Related Tags for Example of struts2.2.1 client side validation.:

Advertisements

Ads

 
Advertisement null

Ads