Home Struts Struts Struts2.2.1 struts2.2.1 validation Interceptor example

Ask Questions?

View Latest Questions


 
 

struts2.2.1 validation Interceptor example
Posted on: February 11, 2011 at 12:00 AM
Advertisement
In this example, We will discuss about the validation Interceptor using struts2.2.1.

struts2.2.1 validation Interceptor example

In this example, We will discuss about the validation Interceptor using struts2.2.1.

In this example,We validate the input using validation Interceptor, The validation Interceptor is used  to validate the input before action is executed. The validation Interceptor validates the values against the Class-validator.xml file and show the field level and action level message. In this example we use three Interceptors, params,workflow,validation. The validation Interceptor checks for implementation of action and workflow interceptor invoke the validate() method of the Action class. The validate() method of Action class validate the input using addFieldError() method.

The Interceptor and action mapping is in the struts.xml file.

Directory structure of example.

 1-Login.jsp  

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

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

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Validation Interceptor Example</title>

<s:head />

</head>

<body bgcolor="lightblue">

<s:actionerror />

<s:form action="Validation">

<s:textfield name="userName" label="User Name"></s:textfield>

<br>

<s:password name="password" label="Password"></s:password>

<s:submit />

</s:form>

</body>

</html>

2-LoginAction.java

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private static final long serialVersionUID = 525429611271529243L;

private String userName;

private String password;

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;

}

public String execute() throws Exception {

return SUCCESS;

}

public void validate() {

if (getUserName().length() == 0) {

addFieldError("userName", "");

} else if (!getUserName().equals("Rose")) {

addFieldError("userName", getText("userName.incorrect"));

}

if (getPassword().length() == 0) {

addFieldError("password", getText("requiredpassword"));

} else if (!getPassword().equals("RoseIndia")) {

addFieldError("password", getText("password.incorrect"));

}

}

}

3-success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

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>Login Successful</title>

</head>

<body bgcolor="lightblue">

<h1>Login Successful</h1>

</body>

</html>

4 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>

<package name="default" extends="struts-default">

 

<action name="Validation" class="roseindia.LoginAction">

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="validation">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<result name="input">/Login.jsp</result>

<result name="success">/success.jsp</result>

</action>

</package>

</struts>

5 LoginAction.properties

username.incorrect = UserName is incorrect.

password.incorrect = Password is incorrect.

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

requiredpassword = ${getText(fieldName)} should be more than 6 character.

 

Login.jsp

required.gif

Incorrect.gif

incomplete.gif

success.gif

Download Select Source Code

Related Tags for struts2.2.1 validation Interceptor example:


DMCA.com