Thursday, August 23, 2012

STRUTS2 Programs 012 Client side validation

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>
--------------------------------------------------------------------------------------------------------------------------------
package.properties
----------------------------------
requiredstring = ${getText(fieldName)} is required.
firstname = Student Name
password= Password


--------------------------------------------------------------------------------------------------------------------------------
RegistrationAction.java
---------------------------------------

package kites.action;

import kites.Model.RegistrationModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegistrationAction extends ActionSupport implements
ModelDriven<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>
----------------------------------------------------------------------------------------------------------------------------
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>

---------------------------------------------------------------------------------------------------------------------------------
RegistrationModel.java
--------------------------------------
package kites.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;
}
}
--------------------------------------------------------------------------------------------------------------------------------
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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<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.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="kites" namespace="/" extends="struts-default">
<action name="registrationForm">
<result>/RegistrationForm.jsp</result>
</action>
<action name="registrationProcess" class="kites.action.RegistrationAction">
<result name="input">RegistrationForm.jsp</result>
<result name="error">RegistrationForm.jsp</result>
<result>RegistrationSuccess.jsp</result>
</action>
</package>
</struts>

--------------------------------------------------------------------------------------------------------------------------------
web.xml
----------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2_Client_Side_Validation_Example</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

------------------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment