Struts 2 Validation Framework

Struts 2 Validation Framework


Posted in : Java Posted on : August 29, 2011 at 5:09 PM Comments : [ 0 ]

In this section, you will learn how to use Struts 2 Validation Framework for validating the form.

Struts 2 Validation Framework

In this section, you will learn how to use Struts 2 Validation Framework for validating the form.

This Struts 2 Validation Framework is provided by XWork. This framework validate the input before processing or execute. It allows us to separate validation logic from the actual java or JSP code so that it can be modified or edited later. It also provide separate routines for client side and server side form validation. If you want to add your own validation logic, you can do this by implementing java interface com.opensymphony.xwork2.Validator and plug it into validation framework as a re-usable component.

The project hierarchy is given below :

 

The jar file used in the application is given below :

Application Flow

First Login form will appear...

After success login, the following page will appears..

When you click on hyperlink following page will appear .This page contains the  validation logic :

The validation rules applied on the above form is given below :

  • The Name, Age, Email & Telephone is mandatory.

  • The age must be in  between 1 to 100 and also must be integer.

  • Email should be in valid format.

The following message will appear if any validation error....

CODE

web.xml (/StrutsLoginValidation/WebContent/WEB-INF/web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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 Application</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>Login.jsp</welcome-file>
</welcome-file-list>

</web-app>

struts.xml (/StrutsLoginValidation/resources/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" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
<action name="login" 
class="com.devmanuals.LoginAction">
<result name="success">LoginSuccess.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="customer" 
class="com.devmanuals.CustomerAction">
<result name="success">SuccessEmployee.jsp</result>
<result name="input">Employee.jsp</result>
</action>
</package>
</struts>

LoginAction.java( /StrutsLoginValidation/src/com/devmanuals/LoginAction.java )

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String username;
private String password;

public String execute() {

if (this.username.equals("admin") 
&& this.password.equals("admin123")) {
return "success";
} else {
addActionError(getText("error.login"));
return "error";
}
//return SUCCESS;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

CustomerAction.java ( /StrutsLoginValidation/src/com/devmanuals/CustomerAction.java )

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport{
private String name;
private Integer age;
private String email;
private String telephone;

public String addCustomer() {
return SUCCESS;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

CustomerAction-validation.xml ( /StrutsLoginValidation/src/com/devmanuals/CustomerAction-validation.xml )

<?xml version="1.0" encoding="UTF-8"?>
<!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="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="errors.required" />
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message key="errors.required" />
</field-validator>
<field-validator type="int">
<param name="min">1</param>
<param name="max">100</param>
<message key="errors.range"/>
</field-validator>
</field>
<field name="email">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
<field-validator type="email">
<message key="errors.invalid" />
</field-validator>
</field>
<field name="telephone">
<field-validator type="requiredstring">
<message key="errors.required" />
</field-validator>
</field>
</validators>

ApplicationResources.properties( /StrutsLoginValidation/resources/ApplicationResources.properties )

label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password. Please try again.

name= Name
age= Age
email= Email
telephone= Telephone
label.add.customer=Add Employee

errors.invalid=${getText(fieldName)} is invalid.
errors.required=${getText(fieldName)} is required.
errors.number=${getText(fieldName)} must be a number.
errors.range=${getText(fieldName)} is not in the range ${min} and ${max}.

Login.jsp ( /StrutsLoginValidation/WebContent/Login.jsp )

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Devmanuals.com-Login Validation Struts Tutorial</title>
</head>

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>

LoginSuccess.jsp ( /StrutsLoginValidation/WebContent/LoginSuccess.jsp )

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>

<body>
<h2>Welcome <s:property value="username" />...!</h2>
<s:a href="Employee.jsp">Add Employee</s:a>
</body>
</html>

Employee.jsp( /StrutsLoginValidation/WebContent/Employee.jsp )

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Employee Form </title>
</head>

<body>
<h2>Employee Form</h2>

<s:form action="customer.action" method="post" validate="true">
<s:textfield name="name" key="name" size="20" />
<s:textfield name="age" key="age" size="20" />
<s:textfield name="email" key="email" size="20" />
<s:textfield name="telephone" key="telephone" size="20" />
<s:submit method="addCustomer" key="label.add.customer" align="center" />
</s:form>
</body>
</html>

SuccessEmployee.jsp ( /StrutsLoginValidation/WebContent/SuccessEmployee.jsp )

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Success Page</title>
</head>

<body>
<h2>Employee Added Successfully.</h2>
</body>
</html>

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics