struts2.2.1 validation Interceptor example
Posted on: February 11, 2011 at 12:00 AM
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.ADS_TO_REPLACE_1

Directory structure of example.

 1-Login.jsp  

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

pageEncoding="UTF-8"%>ADS_TO_REPLACE_2

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

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

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

<html>

<head>

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

<title>Validation Interceptor Example</title>

<s:head />

</head>ADS_TO_REPLACE_5

<body bgcolor="lightblue">

<s:actionerror />

<s:form action="Validation">ADS_TO_REPLACE_6

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

<br>

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

<s:submit />

</s:form>

</body>ADS_TO_REPLACE_8

</html>

2-LoginAction.java

package roseindia;

import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_9

public class LoginAction extends ActionSupport {

private static final long serialVersionUID = 525429611271529243L;

private String userName;ADS_TO_REPLACE_10

private String password;

public String getUserName() {

return userName;ADS_TO_REPLACE_11

}

public void setUserName(String userName) {

this.userName = userName;ADS_TO_REPLACE_12

}

public String getPassword() {

return password;ADS_TO_REPLACE_13

}

public void setPassword(String password) {

this.password = password;ADS_TO_REPLACE_14

}

public String execute() throws Exception {

return SUCCESS;ADS_TO_REPLACE_15

}

public void validate() {

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

addFieldError("userName", "");

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

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

}

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

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

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

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

}ADS_TO_REPLACE_19

}

}

3-success.jsp

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

pageEncoding="UTF-8"%>

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

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

<html>

<head>

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

<title>Login Successful</title>

</head>

<body bgcolor="lightblue">ADS_TO_REPLACE_23

<h1>Login Successful</h1>

</body>

</html>

4 struts.xmlADS_TO_REPLACE_24

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

<!DOCTYPE struts PUBLIC

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

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

<struts>

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

 

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

<interceptor-ref name="params">ADS_TO_REPLACE_27

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

</interceptor-ref>

<interceptor-ref name="validation">ADS_TO_REPLACE_28

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

</interceptor-ref>

<interceptor-ref name="workflow">ADS_TO_REPLACE_29

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

</interceptor-ref>

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

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

</action>

</package>ADS_TO_REPLACE_31

</struts>

5 LoginAction.properties

username.incorrect = UserName is incorrect.

password.incorrect = Password is incorrect.ADS_TO_REPLACE_32

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

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

 

Login.jspADS_TO_REPLACE_33

required.gif

ADS_TO_REPLACE_34

Incorrect.gif

incomplete.gifADS_TO_REPLACE_35

success.gif

ADS_TO_REPLACE_36

Download Select Source Code

Related Tags for struts2.2.1 validation Interceptor example:

Advertisements

Ads

Ads

 
Advertisement null

Ads