87
STRUTS TUTORIAL The JSP Struts Tutorial will help new JSP developers understand the benefits of s What is Struts? Struts is a popular open source framework from Apache Software Foundation Struts offers a set of tag libraries to support the faster development of the differe Struts is basically a Controller. It depends on other things for the model and the Why do we need Struts? Although a dynamic web application can be developed using JSP and Servlet technol " Tightly coupled classes - one change affects other classes Business logic resides in multiple places to find the business rules. Also promotes duplicate business rules. " Data sources are difficult to change as data access methods may be used in the pre " Logic to determine what to do next is across the application,no centralized decision To solve these problems,we need a mechanism that can separate the presentation lay Model, View and Controller " Model - data and logic (business logic methods). " View - output displayed to the user Controller - takes the input from the user and passes it to the relevant model. The mo Advantages of Struts Struts offers many advantages to the application programmer while reducing the dev Master Of Struts 4/29/2009 12:57 PM Page 1 of 87

Master Of Struts

  • Upload
    rajesh

  • View
    1.002

  • Download
    2

Embed Size (px)

DESCRIPTION

struts with Examples

Citation preview

Page 1: Master Of Struts

STRUTS TUTORIAL

The JSP Struts Tutorial will help new JSP developers understand the benefits of struts and how to program their first struts program. Also included are JSP Struts movies to learn Struts by example. Struts source code is also included for download. This is Part 1 of a 3 Part Struts tutorial. See the left hand menu for the other parts when you finish this part of the Struts tutorial.

What is Struts?

Struts is a popular open source framework from Apache Software Foundation to build web applications that integrate with standard technologies such as Servlets, Java Beans and Java Server pages.Struts offers many benefits to the web application developer,including the Model-View-Controller (MVC) design patterns (best practice) in web applications. The Model-View-Controller paradigm applied to web applications lets you separately display code (for example, HTML and tag libraries) from flow control logic (action classes) from the data model to be displayed and updated by the application.

Struts offers a set of tag libraries to support the faster development of the different layers of the web application.The basic idea of the MVC architecture is to divide the application into three layers: Model that represents the data layer, a view layer that represents the data processed by the model component; and a Controller component that is responsible for interaction between the model and the controller. So when we say Struts is an MVC framework for web based applications, we actually mean that it facilitates the rapid development of applications by providing a Controller that helps interaction between the model and the view so that an application developer has not to worry about how to make view and the model independent of each other and yet exist in coordination.

Struts is basically a Controller. It depends on other things for the model and the view. It can communicate with Hibernate, Enterprise Java Beans or any other java components for the model. JSP/Servlets are used for the view which use html front end internally. Struts provides several tag libraries to support the rapid development for the view layer.

Why do we need Struts?

Although a dynamic web application can be developed using JSP and Servlet technology, there are many problems.

" Tightly coupled classes - one change affects other classes Business logic resides in both presentation and data access layer. So if there is a change in a business rule, needsmultiple places to find the business rules. Also promotes duplicate business rules.

" Data sources are difficult to change as data access methods may be used in the presentation layer.

" Logic to determine what to do next is across the application,no centralized decision.To solve these problems,we need a mechanism that can separate the presentation layer of a web application from business logic. Luckily we have MVC and we have Struts. MVC is a design pattern (best practice method) that separates the application design into there main parts:Model, View and Controller

" Model - data and logic (business logic methods).

" View - output displayed to the user

Controller - takes the input from the user and passes it to the relevant model. The model returns and based on the result, the controller decides which view (page) to show the user.

Advantages of Struts

Struts offers many advantages to the application programmer while reducing the development time and making the manageability of the application easier. Here are few key advantages of using Struts instead of managing the every layer of the web application yourself.

Master Of Struts 4/29/2009 12:57 PM

Page 1 of 87

Page 2: Master Of Struts

Centralized File-Based ConfigurationRather than hard coding information into java programs,many Struts values are represented in XML or property files. This loose coupling means that many changes can be made without modifying or recompiling Java code,and that wholesale changes can be made by editing a single file. This approach also lets Java and Web developers focus on their specific tasks (implementing business logic,presenting certain values to clients,etc.) without needing to know about the overallsystem layout.

Form BeansIn JSP,you can use property=* with jsp:setProperty to automatically populate a JavaBean component based on incoming request parameters. Unfortunately,however,in the standard API this capability is unavailable to servlets,even though with MVC it is really servlets,not JSP pages,that should usually be the target of form submissions. Apache Strutsextends this capability to Java code and adds in several useful utilities,all of which serve to greatly simplify the processing of request parameters.

.........................................................................Bean Tags......................................................................Apache Struts provides a set of custom JSP tags (bean:write,in particular) that let you easily output the properties of JavaBeans components. Basically,these are concise and powerful variations of the standard jsp:useBean and jsp:getProperty tags.

................................................................................HTML tagsApache Struts provides a set of custom JSP tags to create HTML forms that are associated with JavaBeans components. This bean/form association serves two useful purposes: It lets you get initial form-field values from Java objects. It lets you redisplay forms with some or all previously entered values intact.

....................................................................Form Field ValidationApache Struts has built-in capabilities for checking that form values are in the required format. If values are missing or in an improper format,the form can be automatically redisplayed with error messages and with the previously entered values maintained.This validation can be performed on the server (in Java),or both on the server and on the client (in JavaScript).

Struts 2.0 is currently in beta phase and contains the existing Struts features plus all the features of WebWorks framework. Setting up Eclipse for Web Development

1. Eclipse does not come with a bundled Java Development Kit. Download and install JDK 1.4 (Sun or IBM) http://java.sun.com/j2se/downloads/index.html2. Download and install Eclipse 3.2.1 from:http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.2.1-200609210945/eclipse-SDK-3.2.1-win32.3pluggins which may be downloaded and installed from there respective repositories. MyEclipse is a mini-IDE which is installed as an add-on to the Eclipse. Download and install MyEclipse 5.1 from: 4. To deploy the web applications,we need a Servlet container like tomcat or JBoss. Download and install Tomcat 5.0.x (or other compliant Servlet/EJB container) from If your Eclipse version is other than 3.2.1,please download an appropriate MyEclipse plug-in from 5. Download the source code used in this tutorialhttp://www.arkaaya.com/tutorials/jsp/struts/sourcecode/eclipse-project.zip

..................................................................Registration Case Study

Master Of Struts 4/29/2009 12:57 PM

Page 2 of 87

Page 3: Master Of Struts

Let us consider an example - a web based user registration application. Usually every web based application requires secure login to access its critical resources therefore a user management is part of every web application. Our first Struts application is a web based user registration application. The application is capable of displaying the list of registered users and allowing the user to add a new user to the database. We'll use a single table 'users' to store the userinformation. Select any database of your choice,and create a table USERS with following fields:

Column Name Data Type ConstraintsUSER_ID TEXT PRIMARY KEYFIRST_NAME TEXT NONELAST_NAME TEXT NONEAGE NUMBER NONEEMAIL TEXT NONEPASSWORD TEXT NONE

We'll use Microsoft Access database for this purpose. Use the following SQL to create table in Microsoft Access.CREATE TABLE USERS(

user_id text primary key,first_name text,last_name text,age number,email text,password text

);

.......................................................Setting up Struts project in EclipseWe are using MyEclipse as a development environment which is an eclipse plugin. Let's create a new project in Eclipse platform and configure it as shown below.Creating an ODBC Data sourceSince Microsoft Access does not have a JDBC driver,we'll connect using the JDBC-ODBC Bridge. Let's create an ODBC DSN first. If you are using a database that supports JDBC driver,you don't need to do this,just replace the connection URL while connecting to the database in next section.

Create the ODBC DSN as shown below.

Configuring Application Server

We have configured the project for struts. But wait…we are missing something. We still don't have an application server ready to deploy our application. Do we have? Let's configure the eclipse for the application server.

...................................Struts Configuration

Now that we have configured our project for struts,let's have a look at the configurations MyEclipse has made for us.

.................................................Web.xml

Master Of Struts 4/29/2009 12:57 PM

Page 3 of 87

Page 4: Master Of Struts

Web.xml resides in application's WEB-INF directory and contains the essential configuration for the web application like the Servlet mappings,Authentications mechanisms,Tag libraries etc.

Let's see what is in web.xml right now.

<?xml version=1.0 encoding=UTF-8?><web-app xmlns=http://java.sun.com/xml/ns/j2ee axmlns:xsi=http://www.w3.org/2001/XMLSchema-instanceversion=2.4 xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee qqhttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>

<servlet><servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><init-param><param-name>config</param-name><param-value>/WEB-INF/struts-config.xml</param-value></init-param><init-param><param-name>debug</param-name><param-value>3</param-value></init-param><init-param><param-name>detail</param-name><param-value>3</param-value></init-param><load-on-startup>0</load-on-startup></servlet><servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>As we can see,there is one Servlet configured and is mapped with URL pattern *.do. What is this 'ActionServlet' for?

This is the simple way how the Struts CONTROLER is plugged into the web application.The basic reason of defining a servlet mapping for a web application is that whenever someone access a page that matches the URL pattern defined in servlet mapping,Application Server will redirect the request to this servlet. That means,we have configure Struts Action Servlet to receive all the request that end with a .do keyword. We can even configure it for .html or .jsp or .abc,.do is the generally accepted token.

Master Of Struts 4/29/2009 12:57 PM

Page 4 of 87

Page 5: Master Of Struts

..................................Struts-config.xml

<?xml version=1.0 encoding=UTF-8?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.2//ENhttp://struts.apache.org/dtds/struts-config_1_2.dtd><struts-config><data-sources /><form-beans /><global-exceptions /><global-forwards /><action-mappings /><message-resources parameter=com.arkaaya.struts.ApplicationResources /></struts-config>

We don't have anything special in this file configured,but as you can see,we have the basic tags. We'll add the appropriate struts components to these tags as we develop them. One important thing in this file is the message-resources. This is a resource bundle that will contain all the errors,labels or other messages that we want to display on the view (jsp pages). The separation of the String messages from the jsp pages helps internationalization.

............................................Writing the User class

Let's start with our first java class. Remember we have a table "users" in the database. Let's write a simple bean that represents this table.

package com.arkaaya.struts.beans;

public class User {private String userId = ;private String firstName = ;private String lastName = ;private int age = 0;private String email = ;private String password = ;public User(String userId) {super();this.userId = userId;}public int getAge() {return age;}

Master Of Struts 4/29/2009 12:57 PM

Page 5 of 87

Page 6: Master Of Struts

public void setAge(int age) {this.age = age;}

public String getEmail() {return email;}

public void setEmail(String email) {this.email = email;}public String getFirstName() {return firstName;}

public void setFirstName(String firstName) {this.firstName = firstName;}

public String getLastName() {return lastName;}

public void setLastName(String lastName) {this.lastName = lastName;}

public String getUserId() {return userId;}

public void setUserId(String userId) {this.userId = userId;}

Master Of Struts 4/29/2009 12:57 PM

Page 6 of 87

Page 7: Master Of Struts

public String getPassword() {return password;}

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

...............................................................ResourceManager

As we have already discussed,one of the main concerns of the struts framework is to separate the string resources from the view. For this purpose,we have already defined a message-resources property n struts-config.xml. Most of the struts components will expect a property key where a string literal will be needed. To be consistent with the struts framework,we will try to separate the string resources from our normal java classes where we are not using struts components. To do this,we can use java.util.ResourceBundle class to pick the values from properties file against thespecified keys. To ease the manipulation of string resources where we are not using the struts components,we'll write a separate class.

package com.arkaaya.struts;import java.util.ResourceBundle;public class ResourceManager {private static ResourceBundle bundle=ResourceBundle.getBundle(com.arkaaya.struts.ApplicationResources);public static String getString(String key){return bundle.getString(key)}}

Writing the Struts Business Component Now let us write our business class that has the ability to perform several actions on the user like adding a new user to the database,updating an existing user,deleting a user or fetching a user from the database. Let us call this class a

........................................UserManager.

package com.arkaaya.struts.db;public class UserManager {private static UserManager mgr = null;private UserManager(){}

Master Of Struts 4/29/2009 12:57 PM

Page 7 of 87

Page 8: Master Of Struts

public static UserManager getInstance(){if(mgr == null){mgr = new UserManager();}return mgr;}}

Because for one application,there can exist only a single UserManager,we have made the UserManager a singleton. We will use UserManager.getInstance() whenever we need an instance of UserManager.

Connecting to a database

The purpose of the UserManager is to provide an interface between the database and the client applications that need access to the database. Let's connect to the database and add the following code in the constructor.

String driverClass = ResourceManager.getString(database.driver);String dbUrl = ResourceManager.getString(database.url);String dbUser = ResourceManager.getString(database.user);String dbPassword = ResourceManager.getString(database.password);try{Class.forName(driverClass);con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);}catch(Exception exp){System.err.println(Could not connect to dtabase.n exp.getMessage());}Where con is a global variable of type java.sql.Connection. Now that we have connected to the database,let us add the necessary methods to manipulate the users. After adding thesemethods,UserManager

package com.arkaaya.struts.db;import java.sql.ResultSet;import java.sql.Statement;

Master Of Struts 4/29/2009 12:57 PM

Page 8 of 87

Page 9: Master Of Struts

import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.PreparedStatement;import java.util.ArrayList;import java.util.List;import com.arkaaya.struts.ResourceManager;import com.arkaaya.struts.beans.User;public class UserManager {private static UserManager mgr = null;private Connection con = null;private UserManager(){String driverClass = ResourceManager.getString(database.driver);String dbUrl = ResourceManager.getString(database.url);String dbUser = ResourceManager.getString(database.user);String dbPassword = ResourceManager.getString(database.password);try{Class.forName(driverClass);con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);}catch(Exception exp){System.err.println(Could not connect to dtabase.n exp.getMessage());}}public void saveUser(User user) throws SQLException{if(user == null)throw new SQLException(ResourceManager.getString(save.user.null));Connection connection = getConnection();PreparedStatement pstmt = connection.prepareStatement(insert intousers(user_id,first_name,last_name,age,email) values(?,?,?,?,?));pstmt.setString(1,user.getUserId());pstmt.setString(2,user.getFirstName());pstmt.setString(3,user.getLastName());pstmt.setInt(4,user.getAge());pstmt.setString(5,user.getEmail());

Master Of Struts 4/29/2009 12:57 PM

Page 9 of 87

Page 10: Master Of Struts

pstmt.executeUpdate();pstmt.close();}

public User getUser(String userId) throws SQLException{if(userId == null || userId.length() == 0)throw new SQLException(ResourceManager.getString(retrieve.user.null));Connection connection = getConnection();Statement stmt = connection.createStatement();ResultSet rs = stmt.executeQuery(select * from users where user_id=&apos; userId &apos;);User user = null;if(rs.next()){user = new User(userId);user.setFirstName(rs.getString(first_name));user.setLastName(rs.getString(last_name));user.setAge(rs.getInt(age));user.setEmail(rs.getString(email));}rs.close();stmt.close();return user;}

public List list() throws SQLException{Connection connection = getConnection();Statement stmt = connection.createStatement();ResultSet rs = stmt.executeQuery(select * from users);User user = null;List list = new ArrayList();while(rs.next()){user = new User(rs.getString(user_id));user.setFirstName(rs.getString(first_name));

Master Of Struts 4/29/2009 12:57 PM

Page 10 of 87

Page 11: Master Of Struts

user.setLastName(rs.getString(last_name));user.setAge(rs.getInt(age));user.setEmail(rs.getString(email));list.add(user);}rs.close();stmt.close();return list;}private Connection getConnection() throws SQLException{if(con == null)throw new SQLException(ResourceManager.getString(database.notConnected));return con;}public static UserManager getInstance(){if(mgr == null){mgr = new UserManager();}return mgr;}

public void finalize(){try{con.close();}catch(Exception exp){}}}

We have used several properties in this class. Add these properties in the ApplicationResources.properties file.

#database connection properties

Master Of Struts 4/29/2009 12:57 PM

Page 11 of 87

Page 12: Master Of Struts

database.driver = sun.jdbc.odbc.JdbcOdbcDriverdatabase.url = jdbc:odbc:usersdatabase.user =database.password =#User operations messagessave.user.null = User is nullretrieve.user.null = User is nulluser.notFound = User not founddatabase.notConnected = Not connected to a databaseNow we have all the business functions available,we are ready to move to the formal development of the struts based application.

Creating the Action Class

The basic component of a struts based application is the Action class. Let us create the action class as shown in the movie. Click the movie below

Adding ActionForward

The forward element in struts-config.xml describes an ActionForward that is to be made available to an Action as a return value. An ActionForward is referenced by a logical name and encapsulates a URI. A forward element may be used to describe both global and local ActionForwards. Global forwards are available to all the Action objects in the module.Local forwards can be nested within an <action> element and only available to an Action object when it is invoked through that ActionMapping. Let us add two forwards for LoginAction "success" and "fail". After adding the two forwards,the <action> tag looks like this:

<actionpath=/logintype=com.arkaaya.struts.action.LoginActionvalidate=false><forward name=success path=/manageusers.jsp /><forward name=failure path=/index.jsp /></action>

Note the two forwards success and the failure. The path attribute in each forward tag is the resource to which the request will be redirected when forwarded to a particular forward. Let us now program our action to return to "success" if the entered user id and password is admin/admin. Right now we don't have any action form associated with this action,let's get the parameters from request as we do in servlets.

String user = request.getParameter(userId);String password = request.getParameter(password);

Master Of Struts 4/29/2009 12:57 PM

Page 12 of 87

Page 13: Master Of Struts

if(user!=null && password!=null && user.equals(admin) && password.equals(admin))return mapping.findForward(success);return mapping.findForward(failure);

Now create index.jsp so that we have a form to submit values to this form. Add the following form in the body section of the jsp.

<form action=/login.do method=post><table border=0><tr><td>Login:</td><td><input type=text name=userId /></td></tr><tr><td>Password:</td><td><input type=text name=password /></td></tr><tr><td colspan=2 align=center><input type=submit value=Login/></td></tr></table></form>

We are almost ready to run the project. But let's create a file manageusers.jsp so that if we enter a valid user id / password,we can see the success page. Add a simple success message "You are logged into this page".

Deploy the Struts ApplicationNow its time to test the magic of our first struts application. First step towards this is to deploy the application to the tomcat server we already configured in this tutorial.

Adding the Form bean

In our LoginAction,we retrieved the form values using the conventional methods in HttpServletRequest. Struts provides an easy way to retrieve these values with the help of form beans. A form bean is a simple JavaBean that extends from org.apache.struts.action.ActionForm. We already have a JavaBean "User" but that does not extend from ActionForm. We can reuse this JavaBean for this purpose. Let's add the extends clause to the User class so that the signature of the class becomes

public class User extends ActionFormAlso we need to add a no-argument constructor to the User class to enable the struts framework to instantiate the bean.

Master Of Struts 4/29/2009 12:57 PM

Page 13 of 87

Page 14: Master Of Struts

public User(){super();}

Configuring Form Bean in struts-config.xml

Adding a form bean is a two step process. First of all we need to declare the form bean in the <form-beans> tag. Then we can associate this form bean to as many actions as we want. To declare the form bean,add the following line in the <form-beans> tag.

<form-bean name=loginForm type=com.arkaaya.struts.beans.User />Now we will associate this form bean with the LoginAction. Update the action tag in action-mappings so that it becomeslike this.<actionname=loginFormpath=/loginscope=requesttype=com.arkaaya.struts.action.LoginActionvalidate=false><forward name=failure path=/index.jsp /><forward name=success path=/manageusers.jsp /></action>Now update the LoginAction to retrieve form values from form bean instead of request object though they are stillavailable in the request object s well. Execute method should look like this now.public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequestrequest,HttpServletResponse response){User userForm = (User)form;String user = userForm.getUserId();String password = userForm.getPassword();if(user!=null && password!=null && user.equals(admin) && password.equals(admin))return mapping.findForward(success);return mapping.findForward(failure);}Again deploy and run the project. You should get the same behavior as you got earlier.

Using Struts Tag Libraries

Master Of Struts 4/29/2009 12:57 PM

Page 14 of 87

Page 15: Master Of Struts

The Struts framework provides a set of built-in Tag libraries that allow you to build the view part of the MVC without embedding Java code directly within your application JSPs. Some commonly used struts tag libraries are described below.

The Bean Tags

The Tags within the Bean Library are used for creating and accessing JavaBeans and a few other general purpose uses. Although these tags work with any standard JavaBean,they are often used with Objects that extend the Struts ActionForm class.

The HTML Tags

The Tags within the Struts HTML Tag Library are used to create input forms for your application. There are also a few other useful Tags used in the creation and rendering of HTML-based user interfaces.

The Logic Tags

The Logic Tag Library contains tags that are helpful with iterating through collections,conditional generation of output,and application flow. We have already used the ActionForm,but we actually have not enjoyed the blessings of struts. Let's program our action to return an appropriate error message called 'ActionError' when the user fails to login successfully. Also re-arrange ourview (the index.jsp) to use the struts tag libraries to manipulate the ActionForm values,and display the appropriate error message on error. After adding the mentioned code,the execute method of LoginAction becomes:

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {User userForm = (User)form;String user = userForm.getUserId();String password = userForm.getPassword();if(user.equals(admin) && password.equals(admin))return mapping.findForward(success);ActionMessages errors = new ActionMessages();ActionMessage error = new ActionMessage(login.failed);errors.add(error,error);saveErrors(request,errors);return mapping.findForward(failure);}

Note that we have created a new ActionMessage with a key "login.failed". This key must exist in the ApplicationResources.properties file. Add the following line in the resource file.login.failed = The supplied user name / password does not match

And the index.jsp becomes:

<%@ page language=java pageEncoding=ISO-8859-1%>

Master Of Struts 4/29/2009 12:57 PM

Page 15 of 87

Page 16: Master Of Struts

<%@taglib prefix=html uri=/WEB-INF/struts-html.tld %><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html><head><title>Welcome to the registration case study</title><!--<link rel=stylesheet type=text/css href=styles.css>--></head><body><html:errors/><html:form action=/login><table border=0><tr><td>Login:</td><td><html:text property=userId /></td></tr><tr><td>Password:</td><td><html:text property=password /></td></tr><tr><td colspan=2 align=center><html:submit value=Login/></td></tr></table></html:form></body></html>

As you can see,we have declared a tag library 'struts-html.tld'. This is the Html tag library we talked about a while ago. The tag <html:errors/> is responsible for displaying the message that we may have saved in the request or the session. We can specify the scope as an attribute to this tag.Now re-deploy the application and see yourself the difference. Did you notice the field values after submitting the invalid values? Yes! The values again appear in the field when theerror message is displayed. This is particularly important when submitting huge forms. And we have achieved all this without any extra programming.

Listing all Users - Struts Iterator Looping

Let us write a simple action and a view that displays the user list on our manageusers.jsp. First of all,create an Action class named 'ListAction' and add the execute method as following.

Master Of Struts 4/29/2009 12:57 PM

Page 16 of 87

Page 17: Master Of Struts

public class ManageUsersAction extends Action {public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {//Get a list of users and save to the requesttry{List list = UserManager.getInstance().list();request.setAttribute(list,list);}catch(SQLException sqle){ActionMessages errors = new ActionMessages();ActionMessage error = new ActionMessage(error.generic,sqle.getMessage());errors.add(error,error);saveErrors(request,errors);}return mapping.findForward(success);}}Add the following line in ApplicationResources.properties file.error.generic = An error prevented the operation. Error detail is:<br>{0}Add the following code in manageusers.jsp<%@ page language=java pageEncoding=ISO-8859-1%><%@taglib uri=/WEB-INF/struts-html.tld prefix=html %><%@taglib uri=/WEB-INF/struts-logic.tld prefix=logic %><%@taglib uri=/WEB-INF/struts-bean.tld prefix=bean %><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html><head><title>Listing all users</title></head><body><center><h3>User List</h3></center><html:errors/><logic:present name=list><table border=0 cellspacing=0 cellpadding=0 align=center width=70% style=border-collapse:collapse;><tr bgcolor=#98AFCC><th>User ID</th>

Master Of Struts 4/29/2009 12:57 PM

Page 17 of 87

Page 18: Master Of Struts

<th>First Name</th><th>Last Name</th><th>Email</th></tr><%boolean even = false; %><logic:iterate id=user name=list><% even = !even; %><tr bgcolor=<%=even?#B7D3F5:#D6E0F5 %>><td><bean:write name=user property=userId /></td><td><bean:write name=user property=firstName /></td><td><bean:write name=user property=lastName /></td><td><bean:write name=user property=email /></td></tr></logic:iterate><tr><td colspan=6 align=center><a href=adduser.jsp>Add New User</a></td></tr></table></logic:present></body></html>

Note that in this jsp,we have not performed any loop and still we are iterating over the list. Also note the separation of view from the model (data). Now We need to update the 'success' forward in struts-config.xml to forward to manageusers.do instead of manageusers.jsp. Also we need to add the manageusers action entry in the struts-config.xml. Now the <action-mappings> looks like this.

<action-mappings ><action

Master Of Struts 4/29/2009 12:57 PM

Page 18 of 87

Page 19: Master Of Struts

name=loginFormpath=/loginscope=requesttype=com.arkaaya.struts.action.LoginActionvalidate=false><forward name=failure path=/index.jsp /><forward name=success path=/manageusers.do /></action><action path=/manageusers type=com.arkaaya.struts.action.ManageUsersAction ><forward name=success path=/manageusers.jsp /></action></action-mappings>

On redeploying and running the application,you should be able to see a list of users on successful login. You don't have any data in the database right now,so you might be viewing a page with only one link "Add New User" but if you add few records manually,you will see the list of users.The variable even is used to indicate if the current row is even or odd to change the row background.

Add User Action - New Struts Action ClassNow it's time to add a new user to the database. Let's write an action first that can save a user in the database. Create a new Action class 'AddUserAction' and add the following code in its execute method.

User user = (User) form;try{UserManager.getInstance().saveUser(user);}catch(SQLException sqle){ActionMessages errors = new ActionMessages();ActionMessage error = new ActionMessage(error.generic,sqle.getMessage());errors.add(error,error);saveErrors(request,errors);}return mapping.findForward(success);Configure this action in the struts-config.xml as given below.<actionattribute=loginFormname=loginFormpath=/adduserscope=request

Master Of Struts 4/29/2009 12:57 PM

Page 19 of 87

Page 20: Master Of Struts

type=com.arkaaya.struts.action.AddUserActionvalidate=false ><forward name=success path=/manageusers.do /></action>Note that we have associated the same form bean that we did when writing the LoginAction.

Add User View

Now it is the time to write a view component for our AddUserAction. Create a jsp page 'adduser.jsp' with the following code.

<%@ page language=java pageEncoding=ISO-8859-1%><%@ taglib uri=/WEB-INF/Struts-html.tld prefix=html %><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html lang=true><head><html:base /><title>Add new user</title></head><body><center><h3>Add New User</h3></center><CENTER><html:errors/></CENTER><html:form action=/adduser method=post><table border=0 align=center><tr><td>User ID:</td><td><html:text property=userId /></td></tr><tr><td>First Name:</td><td><html:text property=firstName /></td></tr><tr><td>Last Name:</td><td><html:text property=lastName /></td></tr><tr>

Master Of Struts 4/29/2009 12:57 PM

Page 20 of 87

Page 21: Master Of Struts

<td>Age:</td><td><html:text property=age /></td></tr><tr><td>Email:</td><td><html:text property=email /></td></tr><tr><td colspan=2 align=center><html:submit /></td></tr></table></html:form></body></html:html>

Run the application,login as admin/admin and click on Add New User link at the bottom of the page. You will find a form. Enter the appropriate values,and click on submit button. You will again be redirected to the page showing the list of users and notice that the new user has been added to the list.

Validating Struts Form Bean

Try to create a user with invalid values. For example an invalid email address that does not follow email addresses rules. You will see that the system does not check the validity of the data and the user still gets inserted into the database. To solve this issue,we need to validate the data before inserting into the database or perform any operation on the data.

We have two options to do this.

1. Perform the validation in action class and return to failure if the validation fails.2. Perform validation in form bean.

The first option is not feasible because one form bean may be used in many actions. So we will need to perform validation in all actions where this form bean is used. The better choice is to perform validation in form bean. Struts framework provides validation facility for form beans. If we need to perform validation on a form bean,we need to override validate method in our ActionForm class. The validate method returns an instance of ActionErrors. We canreturn ActionErrors instance with appropriate ActionError object if the validation fails,or simply return empty ActionErrors instance to indicate that the validation has passed and the struts frame may forward the request to action class.

Let's add the validation to our User class.public ActionErrors validate(ActionMapping mapping,HttpServletRequest request ) {ActionErrors errors = new ActionErrors();if( getUserId().length()<1 ) {errors.add(name,new ActionMessage(null.check,User ID));}

Master Of Struts 4/29/2009 12:57 PM

Page 21 of 87

Page 22: Master Of Struts

if(getEmail().length()>0){ {//Yes! user entered email address. validate itif(!getEmail().matches(^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$errors.add(name,new ActionMessage(email.invalid));}}return errors;}Also we need to enable the validate="true" and provide an input attribute in the action configuration in struts-config.xml<actionattribute=loginFormname=loginFormpath=/adduserscope=requestvalidate=trueinput=/adduser.jsptype=com.arkaaya.struts.action.AddUserAction><forward name=success path=/manageusers.do /></action>

Also we have used two error messages if the validation fails. Add these two keys in the ApplicationResources.properties.

null.check = {0} cannot be null or emptyemail.invalid = Email address does not pass the validation rulesNow run the application and try to create a user with empty User ID or invalid email address. You will see the system does not insert the user into the database. Review of the Struts TutorialLet's review what you have learnt

After reading this tutorial you should have a good idea of how to write a Struts based web application using Eclipse and MyEclipse platform. In addition,we also covered a lot more concepts of what actually the MyEclipse did and how to do them manually even ifwe don't have a sophisticated IDE. After completing this tutorial,with just a few required libraries,a Standard Java Development Kit and an appropriate installation of Tomcat,you should be able to develop and deploy a struts based application. The approach used in this tutorial was using a 'registration' application as a case study. Download the Source Code

What next?Check out our related tutorials on VisualBuilder.com:Struts Tutorial Part 2Struts Tutorial Part 3JSP Tutorial

Master Of Struts 4/29/2009 12:57 PM

Page 22 of 87

Page 23: Master Of Struts

Hibernate TutorialJava Web Component TutorialJSP Design TutorialJava TutorialWe hope you enjoyed reading this tutorial.Be sure to come back to get updates of this tutorial.If you would like to discuss the topics or be kept up to date then sign up to the following groups and edit your emailupdate settings in your groups area in your profile. We will then notify you of updates.JSP GroupIntroduction

This tutorial will help you to learn many features available with the struts framework. You will see how useful are DynaActionForm and DispatchAction classes in struts framework.The tutorial helps you to understand the Validation framework and its usefulness. You will see how the validation framework can be used for both server side and client side validations with less effort. This tutorial also helps you to understand how struts solves the the real life problem of duplicate form submission through its token mechanism. Further this tutorial will help you to understand the exception handling mechanism available with the struts framework and how do we create our own exception handlers in struts. Struts also provide the plug-in mechanism for creating the custom plug-in for the application. This tutorial will also guide you how to create the plug-in for the application in struts.

DispatchAction

The major component in the Struts based application is the Action class. Action class only defines the next steps and logic processing for the application. The execute() method is called by the framework for the processing. All the business logic and all the flow is derived from the execute() method in the Action class. There is a special Action class which will permit the user to have more than one method which can be called by the framework and not just the execute() method. This class is DispatchAction. The class is helpful to have all the methods related to a single UI page in one Action class and developers need not to implement the logics in different classes. The method to be called on Action is decided with the parameter attribute in the <action> tag of struts-config.xml. The code for the mapping is given below.

<action path=/dispatchActionExampletype=com.arkaaya.DispatchActionExampleparameter=methodname=inputForminput=/index.jsp />Note:-The above mapping will check the method parameter and call the method with the name coming in the parameter.Add the following keys in the messageresources.properties file under WEB-INF/classeserror.unspecified= Unspecified Called.error.add= Add Called.error.delete= delete Called.error.view= View Called.Example For DispatchAction(1) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions -->

Master Of Struts 4/29/2009 12:57 PM

Page 23 of 87

Page 24: Master Of Struts

<form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/dispatchActionExampletype=com.arkaaya.DispatchActionExampleparameter=methodname=inputForminput=/index.jsp /></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>DispatchAction Example Continued...(2) ActionForm Classpackage com.arkaaya;import org.apache.struts.action.ActionForm;public class InputForm extends ActionForm{}(3) ActionClassNote:- Now all the action classes must imherit the org.apache.struts.action.DispatchAction Class and not theorg.apache.struts.action.Action Class.package com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionMessages;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;

Master Of Struts 4/29/2009 12:57 PM

Page 24 of 87

Page 25: Master Of Struts

import org.apache.struts.actions.DispatchAction;public class DispatchActionExample extends DispatchAction{protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {ActionMessages message = new ActionMessages();message.add(unspecified,new ActionMessage(error.unspecified));saveMessages(request, message);return mapping.getInputForward();}public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{

ActionMessages message = new ActionMessages();message.add(unspecified,new ActionMessage(error.view));saveMessages(request, message);return mapping.getInputForward();}public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{ActionMessages message = new ActionMessages();message.add(unspecified,new ActionMessage(error.add));saveMessages(request, message);return mapping.getInputForward();}public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {ActionMessages message = new ActionMessages();message.add(unspecified,new ActionMessage(error.delete));saveMessages(request, message);return mapping.getInputForward();}}(4) JSP Page<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean %>

Master Of Struts 4/29/2009 12:57 PM

Page 25 of 87

Page 26: Master Of Struts

<%@ taglib uri=http://struts.apache.org/tags-html prefix=html %><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic %><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent><logic:messagesPresent message=true><html:messages message=true id=msg><p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent><html:form action=/dispatchActionExample.do?method=add method=post><html:submit>Submit for add</html:submit></html:form><html:form action=/dispatchActionExample.do?method=view method=post><html:submit>Submit for view</html:submit></html:form><html:form action=/dispatchActionExample.do?method=delete method=post><html:submit>Submit for delete</html:submit></html:form><html:form action=/dispatchActionExample.do method=post><html:submit>Submit for unspecified</html:submit></html:form></html:html>

Output:-The following buttons will be displayed and on clicking the buttons the text delete called changes

Introduction to DynaActionForm

In struts, every form control need to have one ActionForm class property and getter/setter method for it. DynaActionForm will replace the ActionForm with the simple xml mappings so that the java form class can be eliminated and thus helps in developing the application more quickly. The advantages of using the DynaActionForm are as follows:-1. No ActionForm is required.

Master Of Struts 4/29/2009 12:57 PM

Page 26 of 87

Page 27: Master Of Struts

Replace the property mapping with the simple xml file so that if any property is to add and remove then there is no need to compile the java class again.2. The mapping is to be done in the struts-config.xml file in the <form-bean> tag for all the form properties.<form-bean name=inputForm type=org.apache.struts.action.DynaActionForm ><form-property name=name type=java.lang.String/><form-property name=email type=java.lang.String /></form-bean>Data Types :- The types supported by DynaActionForm include:" java.math.BigDecimal" java.math.BigInteger" boolean and java.lang.Boolean" byte and java.lang.Byte" char and java.lang.Character" java.lang.Class" double and java.lang.Double" float and java.lang.Float" int and java.lang.Integer" long and java.lang.Long" short and java.lang.Short" java.lang.String" java.sql.Date" java.sql.Time" java.sql.TimestampExample For DynaActionForm :-(1) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=org.apache.struts.action.DynaActionForm ><form-property name=name type=java.lang.String/><form-property name=email type=java.lang.String /></form-bean></form-beans><!-- ========================================= Global Exception Definitions -->

Master Of Struts 4/29/2009 12:57 PM

Page 27 of 87

Page 28: Master Of Struts

<global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/dynaActionExampletype=com.arkaaya.DynaActionFormExamplename=inputForminput=/index.jsp /></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>DynaActionForm Example Continued...2) ActionClasspackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionMessages;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;import org.apache.struts.actions.Action;import org.apache.struts.action.DynaActionForm;public class DynaActionFormExample extends Action{public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {DynaActionForm inputForm = (DynaActionForm)form;//Create object of ActionMesssagesActionMessages errors = new ActionMessages();//Check and collect errorsif(((String)inputForm.get(name)).equals()) {errors.add(name,new ActionMessage(error.name.required));

Master Of Struts 4/29/2009 12:57 PM

Page 28 of 87

Page 29: Master Of Struts

}if(((String)inputForm.get(email)).equals()) {errors.add(email,new ActionMessage(error.email.required));}//Saves the errorsaveErrors(request,errors);return mapping.getInputForward();}}(3) JSP Page<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean %><%@ taglib uri=http://struts.apache.org/tags-html prefix=html %><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic %><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent><logic:messagesPresent message=true><html:messages message=true id=msg><p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent><html:form action=/dynaActionExample.do method=post><table><tr><td>Enter the name :- </td><td><html:text property=name/> </td></tr><tr><td>Enter the Email:- </td><td><html:text property=email/></td></tr></table><html:submit>Submit</html:submit></html:form></html:html>Output:-The following will be the output of the application.Struts solution to Duplicate Form Submission (Token mechanism) The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back or when a client accesses a view by returning to a previously book marked page. The struts provide the saveToken() and isTokenValid() to check whether the request is already submitted or not. The method isValidToken() return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it. Returns false under any of the following circumstances:

Master Of Struts 4/29/2009 12:57 PM

Page 29 of 87

Page 30: Master Of Struts

" No session associated with this request" No transaction token saved in the session" No transaction token included as a request parameter" The included transaction token value does not match the transaction token in the user's session

Note:- If the Action class is changed as per the following code then it will validate against the duplicate form submission.public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception{ActionMessages errors = new ActionMessages();if (!isTokenValid(request)) {errors.add(error.duplicaterequest, new ActionMessage(error.duplicaterequest));}resetToken(request);if (!errors.isEmpty()) {saveErrors(request, errors);saveToken(request);return (mapping.getInputForward());}return mapping.findForward(success);}OutputNote:- The output changes as follows :-Introduction to Validation framework

We have seen the validate() method in ActionForm is used to validate the form properties, which are entered by the user on the JSP pages. But each and every validation has to be implemented using the java code .In case the application hassimilar kind of the validations for all the forms then the redundancy is increased. Also updating validation routines arevery cumbersome. The Validator framework was developed by David Winterfeldt as third-party add-on to Struts. Theadvantages of the Validation Framework:-It is XML based framework so it is very easy to modify the validation routines in the xml files as compared to theJava files.1.2. All the validations are at the same place so the reusability of the validations are very easy.3. You can implement the validations in the Client Side as well as Server side with a little coding effort.

Master Of Struts 4/29/2009 12:57 PM

Page 30 of 87

Page 31: Master Of Struts

4. The validation framework is browser independent and needs no extra effort for the different browser in coding.The Components of Validation Framework.The basic components of the Validation Framework is as follows :-" Validations.xml :- This is the basic class consist of all the validation rules applied for a form bean.Validator-rules.xml:- This is the file which contains all the validation rules which can be used to validate theform bean data. A default file is also provided by the Struts framework which is in /org/apache/struts/validator/path and contains the commonly used rules,"Plugin mapping in Struts-Config.xml:- The mapping of the plugin is to be there in the struts-config file so thatStruts controller class will configure the plugin so that it can be used in the application."Simple Example For Validations :-Note :- We are using the default validator-rules.xml for this example.(1) Validations.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE form-validation PUBLIC -//Apache Software Foundation//DTD Commons Validator Rules Configuration1.3.0//EN http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd><form-validation><formset><form name=inputForm><field property=name depends=required><arg key=inputForm.name.required /></field><field property=phone depends=required,mask><arg key=inputForm.phone.required position=0 /><arg key=inputForm.phone.format position=1 /><var><var-name>mask</var-name><var-value>^[0-9]*$</var-value></var></field><field property=email depends=required,email><arg key=inputForm.email.required position=0 /><arg key=inputForm.email.format position=1 /></field></form>

Master Of Struts 4/29/2009 12:57 PM

Page 31 of 87

Page 32: Master Of Struts

</formset></form-validation>(2) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC-//Apache Software Foundation//DTD Struts Configuration 1.3//EN http://struts.apache.org/dtds/struts-config_1_3.dtd<struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/validatetype=com.arkaaya.ValidateActionvalidate=truename=inputForminput=/index.jsp /></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /><!-- =================================================== Validator plugin --><plug-in className=org.apache.struts.validator.ValidatorPlugIn><set-property property=pathnames value=/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validation.xml/></plug-in></struts-config>

Example Continued Validation Framework.(3) Validator Form filepackage com.arkaaya;import org.apache.struts.validator.ValidatorForm;

Master Of Struts 4/29/2009 12:57 PM

Page 32 of 87

Page 33: Master Of Struts

public class InputForm extends ValidatorForm {String name;String email;String phone;public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}(3) Validator Action filepackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class ValidateAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {return mapping.getInputForward();}

Master Of Struts 4/29/2009 12:57 PM

Page 33 of 87

Page 34: Master Of Struts

}(4) JSP file<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html><html:form action=/validate.do method=post><table><tr><td colspan=2><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td colspan=2><logic:messagesPresent message=true><html:messages message=true id=msg><p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td>Enter the name</td><td><html:text property=name /></td></tr><tr><td> Enter the Phone</td><td> <html:text property=phone /></td></tr><tr><td> Enter the Email</td>

Master Of Struts 4/29/2009 12:57 PM

Page 34 of 87

Page 35: Master Of Struts

<td><html:text property=email /></td></tr></table><html:submit>Submit </html:submit></html:form></html:html>

Client Side ValidationEnabling Client Side ValidationThe validation framework also gives the flexibility to use it on Client Side. The framework automatically generates thebrowser specific JavaScript and validate the data only at the client side. Although it has some disadvantages as the clientside validations can be turn off by browser settings and also it is visible to the client. It also has some advantages like itreduces the network congestion as the form is not submitted with the wrong data again and again and only the validateddata is submitted. It is also advisable if you are using the client side validation scripts then also use the validation at theserver side so that two way check will maintain the data integrity fully.The following code if placed in the jsp file will generate the java script at the client side for the specified form based onthe validations.xml file.<html:javascript formName=inputForm/>To call the validation routines call the onSubmit=validateXXX(this) whereas XXX stands for the form name. In our case itis inputForm. So the code is as follows :-<html:form action=/validate.do method=post onSubmit=validateInputForm(this);>Introduction to Standard Validator-Rules.xmlThe Inbuilt validation in the Validator-Rules.xmlThe table will describe the validator rules available with the validator-rules.xml file provided by the StrutsCommunity.

Rule Descriptionbyte Checks value contained by field can be converted to java byte typebyteLocale Checks value contained by field can be converted to java byte type using the numberformatting convention for the current locale. creditCard Checks for the valid card number supplied by major companies like VISA, MASTERCARD etc.date Checks value contained by field can be converted to java.util.Date. double Checks value contained by field can be converted to double primitive type. doubleRange Checks value contained by field can be fitted into the primitive double range or not. email Checks value contained by field can be properly formatted email address or not. float Checks value contained by field can be converted to float primitive type. floatLocale Checks value contained by field can be converted to java float type using the number formatting convention for the current locale.floatRange Checks value contained by field can be fitted into the primitive float range or not.integer Checks value contained by field can be converted to int primitive type. integerLocale Checks value contained by field can be converted to java int type using the numberformatting convention for the current locale. intRange Checks value contained by field can be fitted into the primitive int range or not. long Checks value contained by field can be converted to long primitive type. longLocale Checks value contained by field can be converted to java long type using the number formatting convention for the current locale. longRange Checks value contained by field can be fitted into the primitive long range or not. mask Validate the value with the given Regular Expression. maxLength Validate the maximum length should not be exceeded of the value with the value given in the property. minLength Validate the minimum length should not be exceeded of the value with the value given in the property. required Checks whether the value contain by the field is other than the whitespace. short Checks value contained by field can be converted to java short type shortLocale Checks value contained by field can be converted to java short type using the number formatting convention for the current locale.url Checks the given value is properly formatted URL or not. validwhen Determine the field is validated or not based on the specified test condition.

Standard validator-rules.xml fileThe following is given the standard validator-rules.xml file. In the next section you will learn how to create your own

Master Of Struts 4/29/2009 12:57 PM

Page 35 of 87

Page 36: Master Of Struts

validator rules. Till the time we are using the same validator-rules.xml which is the standard file comes with standardstruts framework.<!--Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICEfile distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to Youunder the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an ASIS BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for thespecific language governing permissions and limitations under the License.--><!DOCTYPE form-validation PUBLIC -//Apache Software Foundation//DTD Commons Validator Rules Configuration1.3.0//EN http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd><!--$Id: validator-rules.xml 481833 2006-12-03 17:32:52Z niallp $This file contains the default Struts Validator pluggable validator definitions. It is contained in struts-core.jar, and shouldbe referenced in the struts-config.xml under the plug-in element for the ValidatorPlugIn.<plug-in className=org.apache.struts.validator.ValidatorPlugIn><set-property property=pathnames value=/org/apache/struts/validator/validator-rules.xml, /WEB-INF/validation.xml/></plug-in>These are the default error messages associated with each validator defined in this file. They should be added to yourprojects ApplicationResources.properties file or you can associate new ones by modifying the pluggable validators msgattributes in this file.# Struts Validator Error Messageserrors.required={0} is required.errors.minlength={0} can not be less than {1} characters.errors.maxlength={0} can not be greater than {1} characters.errors.invalid={0} is invalid.errors.byte={0} must be a byte.errors.short={0} must be a short.errors.integer={0} must be an integer.errors.long={0} must be a long.errors.float={0} must be a float.errors.double={0} must be a double.errors.date={0} is not a date.

Master Of Struts 4/29/2009 12:57 PM

Page 36 of 87

Page 37: Master Of Struts

errors.range={0} is not in the range {1} through {2}.errors.creditcard={0} is an invalid credit card number.errors.email={0} is an invalid e-mail address.Note: Starting in Struts 1.2.0 the default javascript definitions have been consolidated to commons-validator. The defaultcan be overriddenby supplying a <javascript> element with a CDATA section, just as in struts 1.1.--><form-validation><global><validator name=requiredclassname=org.apache.struts.validator.FieldChecksmethod=validateRequiredmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestmsg=errors.required/><validator name=requiredifclassname=org.apache.struts.validator.FieldChecksmethod=validateRequiredIfmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestmsg=errors.required/><validator name=validwhenmsg=errors.requiredclassname=org.apache.struts.validator.validwhen.ValidWhenmethod=validateValidWhenmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,

Master Of Struts 4/29/2009 12:57 PM

Page 37 of 87

Page 38: Master Of Struts

org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequest/><validator name=minlengthclassname=org.apache.struts.validator.FieldChecksmethod=validateMinLengthmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.minlengthjsFunction=org.apache.commons.validator.javascript.validateMinLength/><validator name=maxlengthclassname=org.apache.struts.validator.FieldChecksmethod=validateMaxLengthmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.maxlengthjsFunction=org.apache.commons.validator.javascript.validateMaxLength/><validator name=maskclassname=org.apache.struts.validator.FieldChecksmethod=validateMaskmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequest

Master Of Struts 4/29/2009 12:57 PM

Page 38 of 87

Page 39: Master Of Struts

depends=msg=errors.invalid/><validator name=byteclassname=org.apache.struts.validator.FieldChecksmethod=validateBytemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.bytejsFunctionName=ByteValidations/><validator name=shortclassname=org.apache.struts.validator.FieldChecksmethod=validateShortmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.shortjsFunctionName=ShortValidations/><validator name=integerclassname=org.apache.struts.validator.FieldChecksmethod=validateIntegermethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=

Master Of Struts 4/29/2009 12:57 PM

Page 39 of 87

Page 40: Master Of Struts

msg=errors.integerjsFunctionName=IntegerValidations/><validator name=longclassname=org.apache.struts.validator.FieldChecksmethod=validateLongmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.long/><validator name=floatclassname=org.apache.struts.validator.FieldChecksmethod=validateFloatmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.floatjsFunctionName=FloatValidations/><validator name=doubleclassname=org.apache.struts.validator.FieldChecksmethod=validateDoublemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.double/>

Master Of Struts 4/29/2009 12:57 PM

Page 40 of 87

Page 41: Master Of Struts

<validator name=byteLocaleclassname=org.apache.struts.validator.FieldChecksmethod=validateByteLocalemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.byte/><validator name=shortLocaleclassname=org.apache.struts.validator.FieldChecksmethod=validateShortLocalemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.short/><validator name=integerLocaleclassname=org.apache.struts.validator.FieldChecksmethod=validateIntegerLocalemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.integer/><validator name=longLocaleclassname=org.apache.struts.validator.FieldChecksmethod=validateLongLocale

Master Of Struts 4/29/2009 12:57 PM

Page 41 of 87

Page 42: Master Of Struts

methodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.long/><validator name=floatLocaleclassname=org.apache.struts.validator.FieldChecksmethod=validateFloatLocalemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.float/><validator name=doubleLocaleclassname=org.apache.struts.validator.FieldChecksmethod=validateDoubleLocalemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.double/><validator name=dateclassname=org.apache.struts.validator.FieldChecksmethod=validateDatemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,

Master Of Struts 4/29/2009 12:57 PM

Page 42 of 87

Page 43: Master Of Struts

org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.datejsFunctionName=DateValidations/><validator name=intRangeclassname=org.apache.struts.validator.FieldChecksmethod=validateIntRange

methodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=integermsg=errors.range/><validator name=longRangeclassname=org.apache.struts.validator.FieldChecksmethod=validateLongRangemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=longmsg=errors.range/><validator name=floatRangeclassname=org.apache.struts.validator.FieldChecksmethod=validateFloatRangemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,

Master Of Struts 4/29/2009 12:57 PM

Page 43 of 87

Page 44: Master Of Struts

org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=floatmsg=errors.range/><validator name=doubleRangeclassname=org.apache.struts.validator.FieldChecksmethod=validateDoubleRangemethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=doublemsg=errors.range/><validator name=creditCardclassname=org.apache.struts.validator.FieldChecksmethod=validateCreditCardmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.creditcard/><validator name=emailclassname=org.apache.struts.validator.FieldChecksmethod=validateEmailmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=

Master Of Struts 4/29/2009 12:57 PM

Page 44 of 87

Page 45: Master Of Struts

msg=errors.email/><validator name=urlclassname=org.apache.struts.validator.FieldChecksmethod=validateUrlmethodParams=java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionMessages,org.apache.commons.validator.Validator,javax.servlet.http.HttpServletRequestdepends=msg=errors.url/><!--This simply allows struts to include the validateUtilities into a page, it should not be used as a validation rule.--><validator name=includeJavaScriptUtilitiesclassname=method=methodParams=depends=msg=Creating custom Valdator rulesUsing validation framework in our application not only provide the basic routines for the validations but also gives theuser flexibility to create their own specific validator rules for the application. This is the powerful extensible feature asthis saves a lot more development and validation time during the application development. Below is given the completeset of changes which should be performed while creating the custom validator rules for the application.Steps to create the Validator rules :-1. Create a class and add the static method for the validation.2. Add the entry in the validator-rules.xml file.3. Associate the rule with the form field.4. Add the property in the MessageResources.properties for the error message.ExampleNote:- The example is merged with the Extending the Validation Rules with the inherited SubForm classes. You can seethe marks field which can only take values from 1 to 100. The below is the validation class for the example.package com.arkaaya.validator;import java.io.Serializable;

Master Of Struts 4/29/2009 12:57 PM

Page 45 of 87

Page 46: Master Of Struts

import javax.servlet.http.HttpServletRequest;import org.apache.commons.validator.Field;import org.apache.commons.validator.Validator;import org.apache.commons.validator.ValidatorAction;import org.apache.commons.validator.util.ValidatorUtils;import org.apache.struts.action.ActionMessages;import org.apache.struts.validator.Resources;/*** This class is a custom validator which when apply to the framework checks The* field should contain only alphabets**/public class CustomValidator implements Serializable {public static boolean checkForRange(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validatorvalidator,HttpServletRequest request) {boolean result = false;String value = evaluateBean(bean, field);try {if (Integer.parseInt(value) > 0 && Integer.parseInt(value) < 100) {result = true;}} catch (Exception e) {

}if (!result) {errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));}return result;}private static String evaluateBean(Object bean, Field field) {String value = null;if (bean instanceof String) {value = (String) bean;} else {value = ValidatorUtils.getValueAsString(bean, field.getProperty());

Master Of Struts 4/29/2009 12:57 PM

Page 46 of 87

Page 47: Master Of Struts

}return value;}}Extending the Validator Rules with the inherited SubForm Classes.-2Example Continued....(4) ActionForm ClassesInputForm.javapackage com.arkaaya;import org.apache.struts.validator.ValidatorForm;public class InputForm extends ValidatorForm {String name;String email;String phone;public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}RegistrationForm.javapackage com.arkaaya;public class RegistrationForm extends InputForm {

Master Of Struts 4/29/2009 12:57 PM

Page 47 of 87

Page 48: Master Of Struts

private String Address;private String marks;public String getMarks() {return marks;}public void setMarks(String marks) {this.marks = marks;}public String getAddress() {return Address;}public void setAddress(String address) {this.Address = address;}}(5) Action ClassesValidateAction.javapackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class ValidateAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {return mapping.getInputForward();}}RegisterAction.javapackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;

Master Of Struts 4/29/2009 12:57 PM

Page 48 of 87

Page 49: Master Of Struts

import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class RegisterAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {return mapping.getInputForward();}}(6) OutputInternationalization in struts application Few years back, the sites are only developed using a single language and the developer only create the sites to their specific languages. As the globalization occurs, many frameworks are developed to support the multiple languages at atime. This can be achieved by having the multi language text in key/value pair. And at runtime the text is read from the key as per the language required. This multilingual support is known as Internationalization. So Internationalization is defined as the process of designing an application so that it can be adapted to various languages and regions without engineering changes.The following classes are used to implement Internationalization to any site.Locale - The fundamental Java class that supports internationalization is Locale . Each Locale represents a particular choice of country and language, and also a set of formatting assumptions for things like numbers and dates.

ResourceBundle - The java.util.ResourceBundle class provides the fundamental tools for supporting messages in multiple languages.

PropertyResourceBundle - One of the standard implementations of Resource Bundle allows you to define resources using the same name=value syntax used to initialize properties files. This is very convenient for preparing resource bundles with messages that are used in a web application, because these messages are generally text oriented.

MessageFormat - The java.text.MessageFormat class allows you to replace portions of a message string with arguments specified at run time. This is useful in cases where you are creating a sentence, but the words would appear in a different order in different languages. The placeholder string {0} in the message is replaced by the first runtime argument, {1} is replaced by the second argument, and so on.

MessageResources - The framework class org.apache.struts.util.MessageResources lets you treat a set of resource bundles like a database, and allows you to request a particular message string for a particular Locale instead of for the default Locale the server itself is running in.

The below example will show the use of Internationalization in Struts.Note :- The example only displays the text coming from the different properties files. The properties file MessageResource.properties is created with different locale suffix example fr is for France, de for Germany etc.. So the example is only having the MessagesResources.properties and not the whole program. you can download the complete source from the file. You can change the locale in the IE by tools>internet options>General---Languages and check the

program.(1)MessageResources.propertiesThis is the default properties file. If the locale specific file is not present then struts calls the default properties file.welcome.text=default Welcome Text(2) MessageResources_en.properties welcome.text=English Welcome Text(3) MessageResources_fr.propertieswelcome.text=French Welcome Text(4) MessageResources_de.propertieswelcome.text=German Welcome TextException Handling in Struts

Master Of Struts 4/29/2009 12:57 PM

Page 49 of 87

Page 50: Master Of Struts

Struts 1.3 provides a small, but effective exception-handling framework for the applications. There are two approachesavailable for the exception handling in struts.Declarative:- In this approach the exceptions are defined in the struts-config file and in case of the exceptionoccurs the control is automatically passed to the appropriate error page. The <exception> tag is used to define theexception in the struts-config.xml file. The following are the attributes of the <exception> tag.1.Key:- The key defines the key present in MessageResources.properties file to describe the exception occurred.2. Type:- The class of the exception occurred.3. Path:- The page where the control is to be followed in case exception occurred.Handler:- The exception handler which will be called before passing the control to the file specified in path attribute.There are following two sections in struts-config.xml where you can define the exceptions.4.With Any Action mapping:- In this approach the action class is identified which may throw the exception like password failure, resource access etc.

For example:-<action path=/exceptiontype=com.arkaaya.ExampleActionparameter=methodinput=/index.jsp ><exception key=error.system type=java.lang.RuntimeException path=/index.jsp /></action>"Defining the Exceptions Globally for the struts-config.xml :- If the application control is to pass on a single page for all similar type of exception then the exception can be defined globally. So if in any circumstance the exception occurs the control is passed globally to the exception and control is passed to the error page. For Example:-<global-exceptions><exception key=error.system type=java.lang.RuntimeException path=/index.jsp /></global-exceptions>"1.Programmatic: - In this approach the exceptions are caught using normal java language try/catch block andinstead of showing the exception some meaningful messages are displayed. In this approach the flow of control isalso maintained by the programs. The main drawback of the approach is the developer has to write the code forthe flow of the application.2.

Creating Custom Exception Handlers in Struts-1The struts not only introduced the exception handling mechanism and pass the flow to the appropriate page automatically,it also gives the flexibility to the programmers to create their own Exception Handlers. The struts providesorg.apache.struts.action.ExceptionHandler class for creating the custom exception handlers. All the custom Exception

Master Of Struts 4/29/2009 12:57 PM

Page 50 of 87

Page 51: Master Of Struts

Handlers should extend the ExceptionHandler class and override the execute() method.The below example will demonstrate custom ExceptionHandler for the user program.Note:- The below example will transfer the control to the index.jsp after calling the exception handler. In thestruts-config.xml we are adding the global exception for RuntimeException. You can add any exception like the previousexample to some actions only. The code for adding it as follows :-<exception key=error.system type=java.lang.RuntimeExceptionhandler=com.arkaaya.handler.CustomExceptionHandler path=/index.jsp />Example For ExceptionHandler :-(1) Creating the Exception Handler Classpackage com.arkaaya.handler;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ExceptionHandler;import org.apache.struts.config.ExceptionConfig;public class CustomExceptionHandler extends ExceptionHandler {public ActionForward execute(Exception exception, ExceptionConfig config, ActionMapping mapping, ActionFormformInstance,HttpServletRequest request, HttpServletResponse response) throws ServletException {

74/try {// TODO CustomeCodeSystem.out.println(Exception Handler for the specific error);}catch (Exception e) {}return (super.execute(exception, config, mapping, formInstance, request, response));}}(2) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd>

Master Of Struts 4/29/2009 12:57 PM

Page 51 of 87

Page 52: Master Of Struts

<struts-config><!-- ================================================ Form Bean Definitions --><form-beans></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions><exception key=error.system type=java.lang.RuntimeExceptionhandler=com.arkaaya.handler.CustomExceptionHandler path=/index.jsp /></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/exceptiontype=com.arkaaya.ExampleActioninput=/index.jsp /></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>

Creating Custom Exception Handlers in Struts.-2Exception Handlers in Struts Example Continued ......(3) Creating the Action ClassThe Action class is same as we have learned just with a small change. We are throwing an error from the Action Class. sothe code for the execute method is as follows:-public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {if(true){// Throwing exception to call the Exception Handlerthrow new RuntimeException(No Action);}return mapping.getInputForward();}(4) JSP Page:- index.jsp

Master Of Struts 4/29/2009 12:57 PM

Page 52 of 87

Page 53: Master Of Struts

Note :- The below jsp will only call the Exception.do and then exception handler will pass the control again to this page.<%@ page language=java contentType=text/html; charset=ISO-8859-1pageEncoding=ISO-8859-1%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html><head><meta http-equiv=Content-Type content=text/html; charset=ISO-8859-1><title>Insert title here</title></head><body><form action=exception.do><input type=submit value=Click></form></body></html>

(5) Output:-The program will print Exception Handler for the specific error in the console window which shows that theCustomExceptionHandler is called before redirecting to the specific page.

Creating Plugin for the Struts Application. -1A Plugin is defined as the component which are easily integrated with the applications and helps to enhance thefunctionality of the system. A plug-in is any Java class that is initialized when struts application starts up and destroywhen application shuts down. We have already seen many plugin available for the struts like validations, tiles etc. Thestruts framework also gives the developer flexibility to create their own plugin classes and then implement in the strutsapplications.Example For Custom Plugins :-(1) Creating the Plugin ClassThe struts provide org.apache.struts.action.PlugIn interface. All the custom Plugin should implement the PlugIn interfaceand override the init() and destroy() methods.package com.arkaaya.plugin;import javax.servlet.ServletException;import org.apache.struts.action.ActionServlet;import org.apache.struts.action.PlugIn;

Master Of Struts 4/29/2009 12:57 PM

Page 53 of 87

Page 54: Master Of Struts

import org.apache.struts.config.ModuleConfig;public class CustomPlugin implements PlugIn {public CustomPlugin() {super();}public void destroy() {System.out.println(The plug in Destroyed);}public void init(ActionServlet controller, ModuleConfig config) throws ServletException {controller.getServletContext().setAttribute(testPlugin,Testing for the object);System.out.println(The plug in initialized);}}(2) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans></form-beans><!-- ========================================= Global Exception Definitions -->

<global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/plugintype=com.arkaaya.TestPluginActioninput=/index.jsp ><forward name=SUCCESS path=/index.jsp/></action></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources />

Master Of Struts 4/29/2009 12:57 PM

Page 54 of 87

Page 55: Master Of Struts

<!-- ======================================== Custom Plugin Definitions --><plug-in className=com.arkaaya.plugin.CustomPlugin></plug-in></struts-config>

Creating Plugin for the Struts Application. -2Example Continued....(3) Action Class to Access the Attribute Set in the Pluginpackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class TestPluginAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {System.out.println(servlet.getServletContext().getAttribute(testPlugin));return mapping.findForward(SUCCESS);}}(4) OutputNote:-The program will just print The plug in initialized when the server starts and Print Testing for the object when youcall the /plugin.do action.

WildCard Character mapping for the Actions-1.Now a days the complex application are always bigger in size. So the number of action mappings will also increases.Wildcards can be used to combine similar mappings into one more generic mapping and helps to reduce the size of theconfig files.Wildcard patterns can contain one or more of the following special tokens:* Matches zero or more characters excluding the slash ('/') character.** Matches zero or more characters including the slash ('/') character.character The backslash character is used as an escape sequence. Thus * matches the character asterisk ('*'), and \ matchesthe character backslash ('').

Master Of Struts 4/29/2009 12:57 PM

Page 55 of 87

Page 56: Master Of Struts

Note :-In the action mapping and action forwards, the wildcard-matched values can be accessed with the token {N} whereN is a number from 1 to 9 indicating which wildcard-matched value to substitute. The whole request URI can be accessedwith the {0} token.The action mapping attributes that will accept wildcard-matched strings are:" attribute" catalog" command" forward" include" input" multipartClass" name" parameter" prefix" roles" suffix" typeThe action forward attributes that will accept wildcard-matched strings are:" catalog" command" pathThe below example will demonstrate the Use of wildcard characters.(1) Struts-config.xmlNote:- In the action mapping the parameter is passed as {1}. The jsp which invoked the action replaces the parameter withthe name ofthe jsp in all places.<?xml version=1.0 encoding=ISO-8859-1 ?>

<!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=InputForm type=com.arkaaya.InputForm /><form-bean name=RegisterForm type=com.arkaaya.RegisterForm/></form-beans>

Master Of Struts 4/29/2009 12:57 PM

Page 56 of 87

Page 57: Master Of Struts

<!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/submit*type=com.arkaaya.Submit{1}Actionname={1}Formscope=requestvalidate=falseinput=/{1}.jsp/></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>

WildCard Character mapping for the Actions-2.(2) Action ClassesSubmitRegisterAction.java filepackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;public class SubmitRegisterAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {System.out.println(Called SubmitRegisterAction file );return mapping.getInputForward();}

Master Of Struts 4/29/2009 12:57 PM

Page 57 of 87

Page 58: Master Of Struts

}SubmitInputAction.java filepackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;public class SubmitInputAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {System.out.println(Called SubmitInputAction file );return mapping.getInputForward();}}(3)Action FormsInputForm.java filepackage com.arkaaya;import org.apache.struts.validator.ValidatorForm;public class InputForm extends ValidatorForm {String name;String email;String phone;public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}

public String getName() {return name;

Master Of Struts 4/29/2009 12:57 PM

Page 58 of 87

Page 59: Master Of Struts

}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}RegisterForm.java filepackage com.arkaaya;public class RegisterForm extends InputForm {private String Address;private String marks;public String getMarks() {return marks;}public void setMarks(String marks) {this.marks = marks;}public String getAddress() {return Address;}public void setAddress(String address) {Address = address;}

}

WildCard Character mapping for the Actions-3.(4) Input.jsp File<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%>

Master Of Struts 4/29/2009 12:57 PM

Page 59 of 87

Page 60: Master Of Struts

<%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html><html:form action=/submitInput.do method=post><table><tr><td colspan=2><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td colspan=2><logic:messagesPresent message=true><html:messages message=true id=msg><p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td>Enter the name</td><td><html:text property=name /></td></tr><tr><td> Enter the Phone</td><td> <html:text property=phone /></td></tr><tr><td> Enter the Email</td><td><html:text property=email /></td></tr></table><html:submit>Submit </html:submit></html:form>

Master Of Struts 4/29/2009 12:57 PM

Page 60 of 87

Page 61: Master Of Struts

</html:html>(5) Regsiter.jsp File<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html>

<html:form action=/submitRegister.do method=post><table><tr><td colspan=2><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td colspan=2><logic:messagesPresent message=true><html:messages message=true id=msg><p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td>Enter the name</td><td><html:text property=name /></td></tr><tr><td> Enter the Phone</td><td> <html:text property=phone /></td></tr>

Master Of Struts 4/29/2009 12:57 PM

Page 61 of 87

Page 62: Master Of Struts

<tr><td> Enter the Email</td><td><html:text property=email /></td></tr><tr><td> Enter the Address </td><td><html:text property=address /></td></tr><tr><td> Enter the Marks </td><td><html:text property=marks /></td></tr></table><html:submit>Submit </html:submit></html:form></html:html>(6) OutputNote:- When user submits the Register form and calls the submitRegister.do action then automatically the control callsthe submitRegisterAction class and prints Called SubmitRegisterAction file and same as for the submitInput.do calls thesubmitInputAction class and prints Called SubmitInputAction file on the console.

Uploading the Files to Server Using Struts-1Many applications gives the utility to upload the data from client applications to the server. Struts also gives the flexibilityto upload the files using the org.apache.struts.upload.FormFile class. The org.apache.struts.upload.FormFile class is aspecial class in struts which represent the property type of file. The following example will demonstrate to upload any txtfile and place it in the upload folder by the name of temp.txt.Example:-(1) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings>

Master Of Struts 4/29/2009 12:57 PM

Page 62 of 87

Page 63: Master Of Struts

<actionpath=/submittype=com.arkaaya.InputActionname=inputFormscope=requestvalidate=falseinput=/index.jsp/></action-mappings><controller maxFileSize=2M /><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>(2) InputForm.java Filepackage com.arkaaya;

import org.apache.struts.upload.FormFile;import org.apache.struts.validator.ValidatorForm;public class InputForm extends ValidatorForm {private FormFile fileName;public FormFile getFileName() {

Uploading the Files to Server Using Struts-2(3) InputAction.java filepackage com.arkaaya;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.Globals;import org.apache.struts.action.Action;

Master Of Struts 4/29/2009 12:57 PM

Page 63 of 87

Page 64: Master Of Struts

import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;import org.apache.struts.upload.FormFile;public class InputAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {InputForm inputForm = (InputForm) form;// retrieve the file representationFormFile file = inputForm.getFileName();// retrieve the file nameString fileName = file.getFileName();// retrieve the content type

Introduction to Modules in Struts Application

Generally, all the applications and systems are modularized so that the time of development and complexity can bereduced. The struts also give the flexibility to have more than one independent modules in the application. The separatestruts-config files are introduced for each module. This helps developers to develop the complex applications moreeffectively as different independent applications can be developed separately and then combined together to make acomplete system. The struts gives a special Action class, SwitchAction , which is used for the switching between themodules. The below example will demonstrate the modules in the Struts Applications.The following things needs to be changed to make more than one modules in the application." All the module specific config files should be kept on the module specific directories under WEB-INF folder." All the jsp files for the module should be kept under the module folder." All the module specific files should be passed in the ActionServlet in the Web.xml file.There are three approaches for switching from one module to another." You can use the org.apache.struts.actions.SwitchAction from the Extras JAR," you can use a <forward> (global or local) and specify the contextRelative attribute with a value of true,or you can specify the module parameter as part of any of the Struts JSP hyperlink tags (Include, Img, Link,Rewrite, or Forward)."

Master Of Struts 4/29/2009 12:57 PM

Page 64 of 87

Page 65: Master Of Struts

Note:- The coming example will demonstrate the all three approaches for the application

Modules Example-1Note:- In the below example we are creating the admin and register module. You can download the whole source fromdownload link. We only show the code which is written for the modules so that you can understand the exact logic toswitch the modules.(1) Web.xml file Changes<servlet><servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><init-param><param-name>config</param-name><param-value>/WEB-INF/struts-config.xml</param-value></init-param><init-param><param-name>config/admin</param-name><param-value>/WEB-INF/admin/struts-admin.xml</param-value></init-param><init-param><param-name>config/register</param-name><param-value>/WEB-INF/register/struts-register.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet>(2) Struts-config.xml for default Module.<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans></form-beans><!-- ========================================= Global Exception Definitions -->

Modules Example-2

Master Of Struts 4/29/2009 12:57 PM

Page 65 of 87

Page 66: Master Of Struts

(5) index.jsp file for Default Module.<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html>This is the default Module Application<br/>Links created for own Forward action <br/><a href=switchAdmin.do>Admin</a><br/><a href=switchRegister.do>Register</a><br/>Links created for the SwitchAcion<br/><a href=switchtoAdmin.do?prefix=/admin&page=/index.jsp>Admin</a><br/><a href=switchtoRegister.do?prefix=/regsiter&page=/index.jsp>Register</a><br/>Links created for the html:link tag<br/><html:link module=/admin page=/index.jsp>admin</html:link><html:link module=/register page=/index.jsp>register</html:link><br/></html:html>(6) index.jsp file for Admin Module.<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html>This is the Admin Module File<a href=/switchToRegister.do?prefix=/register&page=/index.jsp>Register</a></html:html>(7) index.jsp file for Register Module.<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html>This is the Regsiter Module File<a href=/switchToAdmin.do?prefix=/admin&page=/index.jsp>Admin</a>

Master Of Struts 4/29/2009 12:57 PM

Page 66 of 87

Page 67: Master Of Struts

</html:html>

(8) Output:-Note:- On running the default module index.jsp file the following output will be displayed on the screen.

Customizing the ActionServlet Class for the application-1.

Struts follow the Model-View-Controller pattern. ActionServlet provides the controller in the MVC design pattern forweb applications that is commonly known as Model 2. The standard version of ActionServlet is configured based on thefollowing servlet initialization parameters,config - Comma-separated list of context-relative path(s) to the XML resource(s) containing the configurationinformation for the default module. (Multiple files support since Struts 1.1) [/WEB-INF/struts-config.xml]."config/${module} - Comma-separated list of Context-relative path(s) to the XML resource(s) containing theconfiguration information for the module that will use the specified prefix (/${module}). This can be repeated asmany times as required for multiple modules."configFactory - The Java class name of the ModuleConfigFactory used to create the implementation of theModuleConfig interface."convertNull - Force simulation of the Struts 1.0 behavior when populating forms. If set to true, the numeric Javawrapper class types (like java.lang.Integer ) will default to null (rather than 0). (Since Struts 1.1) [false]"rulesets - Comma-delimited list of fully qualified classnames of additional org.apache.commons.digester.RuleSetinstances that should be added to the Digester that will be processing struts-config.xml files. By default, only theRuleSet for the standard configuration elements is loaded."validating - Should we use a validating XML parser to process the configuration file (strongly recommended)?[true]"chainConfig - Comma-separated list of either context-relative or classloader path(s) to load commons-chaincatalog definitions from. If none specified, the default Struts catalog that is provided with Struts will be used."The main power of struts is that you can customize the controller behaviour if needed by your application. The belowexample will demonstrate to override the default functionality of the controller and do some application specific

Master Of Struts 4/29/2009 12:57 PM

Page 67 of 87

Page 68: Master Of Struts

processing before and after the request has been processed.(1) Struts-config.xml file<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><actionpath=/submittype=com.arkaaya.SubmitActionname=inputFormscope=requestvalidate=false

input=/index.jsp/></action-mappings><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources />

Customizing the ActionServlet Class for the application-2.

web.xml fileThe mapping for the ActionServlet class is written in the web.xml file so we have to change it for mapping ourCustomActionServlet class.<?xml version=1.0 encoding=UTF-8?><web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

Master Of Struts 4/29/2009 12:57 PM

Page 68 of 87

Page 69: Master Of Struts

<servlet><servlet-name>action</servlet-name><servlet-class>com.arkaaya.CustomActionServlet</servlet-class><init-param><param-name>config</param-name><param-value>/WEB-INF/struts-config.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>(3)CustomActionServlet.java fileThis is the file which is the replaces the Struts Controller class. We extends the Struts ActionServlet to get the defaultbehaviour and override the process method.package com.arkaaya;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionServlet;public class CustomActionServlet extends ActionServlet {private static final long serialVersionUID = 1L;public void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{//TODO You can put the Code before the request handling.System.out.println(Before Handling Request);super.process(request,response);//TODO You can put the Code after the request handling.System.out.println(After Handling Request);}

Master Of Struts 4/29/2009 12:57 PM

Page 69 of 87

Page 70: Master Of Struts

}(4)InputForm.java filepackage com.arkaaya;import org.apache.struts.validator.ValidatorForm;public class InputForm extends ValidatorForm {String name;String email;String phone;public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}

Customizing the ActionServlet Class for the application-3.

(5) SubmitAction.java filepackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;

Master Of Struts 4/29/2009 12:57 PM

Page 70 of 87

Page 71: Master Of Struts

import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;public class SubmitAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws Exception {System.out.println(Called Submit file );return mapping.getInputForward();}}(6)Index.jsp file<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html><html:form action=/validate.do method=post><table><tr><td colspan=2><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td colspan=2><logic:messagesPresent message=true><html:messages message=true id=msg>

Master Of Struts 4/29/2009 12:57 PM

Page 71 of 87

Page 72: Master Of Struts

<p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td>Enter the name</td><td><html:text property=name /></td></tr><tr><td> Enter the Phone</td><td> <html:text property=phone /></td></tr><tr><td> Enter the Email</td><td><html:text property=email /></td></tr></table><html:submit>Submit </html:submit></html:form></html:html>(7)OutputNote:- Whenever the request is submitted to the application you see two line Before Handling Request and After HandlingRequest printing on the console and the line Called Submit file is printed in between. This shows that for every request thecode of CustomActionServlet gets called.

Customizing the ActionServlet Class for the application-3.

(5) SubmitAction.java filepackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;public class SubmitAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)

Master Of Struts 4/29/2009 12:57 PM

Page 72 of 87

Page 73: Master Of Struts

throws Exception {System.out.println(Called Submit file );return mapping.getInputForward();}}(6)Index.jsp file<%@ taglib uri=http://struts.apache.org/tags-bean prefix=bean%><%@ taglib uri=http://struts.apache.org/tags-html prefix=html%><%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%><!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN><html:html><html:form action=/validate.do method=post><table><tr><td colspan=2><logic:messagesPresent><html:messages id=msg><p><strong><font color=red><bean:write name=msg /></font></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td colspan=2><logic:messagesPresent message=true><html:messages message=true id=msg><p><strong><bean:write name=msg /></strong></p></html:messages></logic:messagesPresent></td></tr><tr><td>Enter the name</td>

Master Of Struts 4/29/2009 12:57 PM

Page 73 of 87

Page 74: Master Of Struts

<td><html:text property=name /></td></tr><tr><td> Enter the Phone</td><td> <html:text property=phone /></td></tr><tr><td> Enter the Email</td><td><html:text property=email /></td></tr></table><html:submit>Submit </html:submit></html:form></html:html>(7)OutputNote:- Whenever the request is submitted to the application you see two line Before Handling Request and After HandlingRequest printing on the console and the line Called Submit file is printed in between. This shows that for every request thecode of CustomActionServlet gets called.

Customizing RequestProcessor Class for the application-1.

The previous example has shown you to customize the controller before and after any request processed. But Strutsprovide a class named RequestProcessor which actually done all the processing for a particular request. In Struts 1.0 thewhole things are done by the ActionServlet class. But with the next release the request processing code shifted toRequestProcessor class. RequestProcessor contains the processing logic that the ActionServlet performs as it receiveseach servlet request from the container. You can customize the request processing behavior by subclassing this class andoverriding the method(s) whose behavior you are interested in changing. the following methods are available in theRequestProcessor Class." processMultipart():- Handles the multipart request containing the images etc." processPath():- Identify and return the path component" processLocale():- Automatically select a Locale for the current user, if requested." processContent():- Set the default content type with character encodings for all responses" processNoCache():- Set the no-cache headers for all responses, if requested." processPreprocess():- General-purpose preprocessing hook that can be overridden as required by subclasses." processMapping():- Select the mapping used to process the selection path for this request.processRoles():- If this action is protected by security roles, make sure that the current user possesses at leastone of them."processActionForm():- Retrieve and return the ActionForm associated with this mapping, creating and retainingone if necessary."

Master Of Struts 4/29/2009 12:57 PM

Page 74 of 87

Page 75: Master Of Struts

processPopulate():- Populate the properties of the specified ActionForm instance from the request parametersincluded with this request."processValidate():- If this request was not cancelled, and the request's ActionMapping has not disabledvalidation, call the validate method of the specified ActionForm, and forward to the input path if there were anyerrors."" processForward():- Process a forward requested by this mapping (if any)." processInclude():- Process an include requested by this mapping (if any).processActionCreate():- Return an Action instance that will be used to process the current request, creating anew one if necessary."" processActionPerform():- Ask the specified Action instance to handle this request." processForwardConfig():- Forward or redirect to the specified destination, by the specified mechanism.Note:- The below example will demonstrate the overriding of methods in the RequestProcessor class. The example willoverride processPreprocess() method of the RequestProcessor Class. >(1)Struts-config.xml fileNote - RequestProcessor mapping is given by the <controller> tag in the struts-config.xml file.<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><actionpath=/submittype=com.arkaaya.SubmitActionname=inputForm

Master Of Struts 4/29/2009 12:57 PM

Page 75 of 87

Page 76: Master Of Struts

scope=requestvalidate=falseinput=/index.jsp/></action-mappings><!-- =========================================== Controller Mapping Definition --><controller contentType=text/html;charset=UTF-8 locale=true debug=1 nocache=trueprocessorClass=com.arkaaya.CustomRequestProcessor /><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>

Customizing RequestProcessor Class for the application.-2

(2) CustomRequestProcessor.java filepackage com.arkaaya;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.RequestProcessor;public class CustomRequestProcessor extends RequestProcessor {public boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {System.out .println(Called the preprocess method before processing the request);return super.processPreprocess(request,response);}}Note:- Only the above specified files are addition to the previous example i.e.Customizing the ActionServlet Class for theapplication.(3) OutputThe text Called the preprocess method before processing the request gets printed each time the request is processed by theStruts. This shows that it is calling the CustomRequestProcessor and not the RequestProcessor class.

ComposableRequestProcessor in Struts 1.3

Introduction :- A popular technique for organizing the execution of complex processing flows is the Chain ofResponsibility pattern. In this pattern the different "commands" are combined into a single chain with a series ofprocessing elements and the context parameter is passed along with all the parameter. All the commands contain a singleexecute method. A Boolean variable determines whether the command executes successfully or not. This pattern promotes

Master Of Struts 4/29/2009 12:57 PM

Page 76 of 87

Page 77: Master Of Struts

the idea of loose coupling, a common programming practice.From the version 1.3, the struts replaces the RequestProcessor with ComposableRequestProcessor class which follows theChain of Responsibility pattern (CoR) design pattern. It is configured via the following context initialization parameters:org.apache.struts.chain.CATALOG_NAME - Name of the Catalog in which we will look up the Command to beexecuted for each request. If not specified, the default value is struts."org.apache.struts.chain.COMMAND_NAME - Name of the Command which we will execute for each request,to be looked up in the specified Catalog. If not specified, the default value is servlet-standard."Each step of the processing is represented as a separate Command in the Chain. User can easily customize by changing,removing and replacing the chain commands. The Command classes are following packages:-1. org.apache.struts.chain.commands.2. org.apache.struts.chain.commands.servlet packages.The following table will listed out all the command objects of CoR.Command DescriptionSelectLocale Select a locale for this request, if one hasn't already been selected, and place it in therequest.SetOriginalURI Store the URI of the original request in the request.RequestNoCache If appropriate, set the following response headers: Pragma, Cache-Control, and Expires.RemoveCachedMessagesRemoves any ActionMessages object stored in the session under Globals.MESSAGE_KEYand Globals.ERROR_KEY if the messages' isAccessed method returns true. This allowsmessages to be stored in the session, displayed one time, and be released.SetContentType Set the default content type (with optional character encoding) for all responses if requested.SelectAction Determine the ActionMapping associated with this path.AuthorizeAction If the mapping has a role associated with it, ensure the requesting client has the specifiedrole. If the client does not, raise an error and stop processing of the request.CreateActionForm Instantiate (if necessary) the ActionForm associated with this mapping (if any) and place itinto the appropriate scope.PopulateActionForm Populate the ActionForm associated with this request, if any.ValidateActionForm Perform validation (if requested) on the ActionForm associated with this request (if any).SelectInput If validation failed, select the appropriate ForwardConfig for return to the input page.ExecuteCommand Lookup and execute a chain command if the current ActionConfig is so-configured.SelectForward If this mapping represents a forward, forward to the path specified by the mapping.SelectInclude Select the include uri (if any) for the current action mapping.PerformInclude If selected, include the result of invoking the path in this request.

Master Of Struts 4/29/2009 12:57 PM

Page 77 of 87

Page 78: Master Of Struts

CreateAction Instantiate an instance of the class specified by the current ActionMapping (if necessary).ExecuteAction This is the point at which your Action's execute method will be called.ExecuteForwardCommand Lookup and execute a chain command if the current ForwardConfig is so-configured.PerformForwardFinally, the process method of the RequestProcessor takes the ActionForward returned byyour Action class, and uses it to select the next resource (if any). Most often theActionForward leads to the presentation page that renders the response.The default Chain-config file supplied by the apache is as follows:-<?xml version=1.0 ?><!--$Id: chain-config.xml 481833 2006-12-03 17:32:52Z niallp $Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the License); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an AS IS BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.--><!--This file contains definitions of the standard Chain Of Responsibility chains that emulate Struts 1.2 processingfunctionality. These chains are defined in a catalog named struts so that the application can use the default catalog for itsown purposes, without any potential for name clashes.$Id: chain-config.xml 481833 2006-12-03 17:32:52Z niallp $--><catalog name=struts><define name=lookup className=org.apache.commons.chain.generic.LookupCommand/><!-- ========== Servlet Complete Request Chain ========================= --><chain name=servlet-standard><!-- Establish exception handling filter --><command className=org.apache.struts.chain.commands.ExceptionCatcher catalogName=struts

Master Of Struts 4/29/2009 12:57 PM

Page 78 of 87

Page 79: Master Of Struts

exceptionCommand=servlet-exception/><lookup catalogName=struts name=process-action optional=false/><lookup catalogName=struts name=process-view optional=false/></chain><!-- ========== Action Processing chain ======================== --><chain name=process-action><!--This chain attempts to emulate (most of) the standard request processing in the standardorg.apache.struts.action.RequestProcessor class, by performing the corresponding tasks in individual Commandsthat are composable. The following list defines a cross reference between the processXxx methods and the Commandsthat perform the corresponding functionality:processMultipart Integrated into servlet and legacy classesprocessPath SelectAction (which also does processMapping)processException ExceptionCatcher / ExceptionHandlerprocessLocale SelectLocaleprocessContent SetContentTypeprocessNoCache RequestNoCacheprocessPreprocess LookupCommand with optional=true. Multiple occurrences of this can easily be added, to supportadditional processing hooks at any point in the chain without modifying the standard definition.processCachedMessages RemoveCachedMessagesprocessMapping SelectAction (which also does processPath)processRoles AuthorizeActionprocessActionForm CreateActionFormprocessPopulate PopulateActionFormprocessValidate ValidateActionForm / SelectInputprocessForward SelectForwardprocessInclude SelectInclude / PerformIncludeprocessActionCreate CreateActionprocessActionPerform ExecuteAction--><!-- Look up optional preprocess command --><lookup catalogName=struts name=servlet-standard-preprocess optional=true/><!-- Identify the Locale for this request --><command className=org.apache.struts.chain.commands.servlet.SelectLocale/><!-- Set (if needed) the URI of the original request --><command className=org.apache.struts.chain.commands.servlet.SetOriginalURI/>

Master Of Struts 4/29/2009 12:57 PM

Page 79 of 87

Page 80: Master Of Struts

<!-- Set (if needed) no cache HTTP response headers --><command className=org.apache.struts.chain.commands.servlet.RequestNoCache/><!-- Set (if needed) the HTTP response content type --><command className=org.apache.struts.chain.commands.servlet.SetContentType/><!-- Remove messages cached in the Session --><command className=org.apache.struts.chain.commands.RemoveCachedMessages/><!-- Identify the ActionConfig for this request --><command className=org.apache.struts.chain.commands.servlet.SelectAction/><!-- Authorize the selected ActionConfig for this request --><command className=org.apache.struts.chain.commands.servlet.AuthorizeAction/><!-- Create (if needed) the ActionForm for this request --><command className=org.apache.struts.chain.commands.CreateActionForm/><!-- Populate the ActionForm for this request --><command className=org.apache.struts.chain.commands.servlet.PopulateActionForm/><!-- Validate the ActionForm for this request --><command className=org.apache.struts.chain.commands.servlet.ValidateActionForm/><!-- Select the appropriate ForwardConfig for return to input page --><command className=org.apache.struts.chain.commands.servlet.SelectInput/><!-- Lookup and execute a chain command if the current ActionConfig is so-configured. --><command className=org.apache.struts.chain.commands.ExecuteCommand/><!-- Select the appropriate ForwardConfig for action mappings that only have an ActionForward --><command className=org.apache.struts.chain.commands.servlet.SelectForward/><!-- Select the include uri (if any) for the current action mapping --><command className=org.apache.struts.chain.commands.SelectInclude/><!-- Perform the include (if needed) --><command className=org.apache.struts.chain.commands.servlet.PerformInclude/><!-- Create (if needed) the Action for this request --><command className=org.apache.struts.chain.commands.servlet.CreateAction/><!-- Execute the Action for this request --><command className=org.apache.struts.chain.commands.servlet.ExecuteAction/></chain><!-- ========== View Processing chain ======================== --><chain name=process-view><!-- Lookup and execute a chain command if the current ForwardConfig is so-configured. --><command className=org.apache.struts.chain.commands.ExecuteForwardCommand/><!-- Follow the returned ForwardConfig (if any) -->

Master Of Struts 4/29/2009 12:57 PM

Page 80 of 87

Page 81: Master Of Struts

<command className=org.apache.struts.chain.commands.servlet.PerformForward/></chain><!-- ========== Servlet Exception Handler Chain ======================== --><chain name=servlet-exception><!--This chain is designed to be invoked (by o.a.s.c.ExceptionCatcher) if an unhandled exception is thrown by any subsequentcommand in a processing chain (including the one that invokes a Struts action). The standard definition of this chainsupports the exception mapping of Struts 1.1, but can be replaced in order to handle exceptions differently.--><!-- Execute the configured exception handler (if any) --><command className=org.apache.struts.chain.commands.servlet.ExceptionHandler/><!-- Follow the returned ForwardConfig (if any) --><command className=org.apache.struts.chain.commands.servlet.PerformForward/></chain></catalog>

Adding New Command Objects in Struts CoR Pattern

The Struts new CoR(Chain of Responsibility) structure gives us more flexibility to add our commands in between therequest processing and also with less effort. All the user needs is to extendorg.apache.struts.chain.commands.ActionCommandBase class and override the execute() method in it.The below example will demonstrate how do we insert our own command objects in the existing Struts CoR withoutchanging the actual flow and the ComposeableRequestProcessor will call the file implicitly. For any type of addition anddeletion of the commands only the chain-config.xml file is changed and the Struts automatically take care of the flow.(1) Struts-config.xml<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards>

Master Of Struts 4/29/2009 12:57 PM

Page 81 of 87

Page 82: Master Of Struts

<!-- =========================================== Action Mapping Definitions --><action-mappings><actionpath=/submittype=com.arkaaya.SubmitActionname=inputFormscope=requestvalidate=falseinput=/index.jsp/></action-mappings><!-- =========================================== Controller Mapping Definition --><controller contentType=text/html;charset=UTF-8 locale=true debug=1 nocache=trueprocessorClass=org.apache.struts.chain.ComposableRequestProcessor /><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>(2) CustomCommand.java FileNote:- The following file is created for the command object. If it returns true it means some exception occurred so it wontcall the next Command in the chain and returns to the input jsp file.package com.arkaaya.command;import org.apache.struts.chain.commands.ActionCommandBase;import org.apache.struts.chain.contexts.ActionContext;public class CustomCommand extends ActionCommandBase {@Overridepublic boolean execute(ActionContext ctx) throws Exception {boolean result=true;System.out.println(Called the logic for the Custom Command);return false;}}(3) Chain-config.xml fileNote :- The following command will be added in the chain-config.xml file anywhere in the process-action chain.<!-- Execute the Custom Command for this request --><command className=com.arkaaya.command.CustomCommand/>

SECURITY IN STRUTS

Master Of Struts 4/29/2009 12:57 PM

Page 82 of 87

Page 83: Master Of Struts

Implementation For Container Managed Implementation For Application ManagedAuthentication and Authorization are specified inweb.xml."It uses multiple authentication schemes, such asPassword Authentication Form-basedAuthentication Client side Digital Certificates etc.."" Redirects are handled automatically.User data can be provided by a variety of stores xmlfile or flat files. In tomcat the Data is provided inTOMCAT_HOME/conf/tomcat-users.xml"Extending RequestProcessor (in previousversions)or AuthorizeAction (After 1.3)."" Cookies" Using Servlet Filters." Using SSLEXT with Struts to enable HTTPS.The following is the Example to implement the Container Specific Security in Tomcat.(1) tomcat-user.xml File<?xml version='1.0' encoding='utf-8'?><tomcat-users><role rolename=tomcat/><role rolename=role1/><role rolename=admin/><user username=tomcat password=tomcat roles=tomcat/><user username=both password=tomcat roles=tomcat,role1/><user username=role1 password=tomcat roles=role1/><user username=visualbuilder password=test roles=admin/></tomcat-users>(2) Web.xml file<?xml version=1.0 encoding=UTF-8?><web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

Master Of Struts 4/29/2009 12:57 PM

Page 83 of 87

Page 84: Master Of Struts

<servlet><servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><init-param><param-name>config</param-name><param-value>/WEB-INF/struts-config.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><security-constraint><web-resource-collection><web-resource-name>application</web-resource-name><url-pattern>/*</url-pattern></web-resource-collection><auth-constraint><role-name>admin</role-name></auth-constraint></security-constraint><login-config><auth-method>BASIC</auth-method><realm-name>securityapp</realm-name></login-config><security-role><description>Testing the Application Security</description><role-name>admin</role-name></security-role></web-app>Output:-The following screen appears when you try to run the application. It will ask for username and password and once you

Master Of Struts 4/29/2009 12:57 PM

Page 84 of 87

Page 85: Master Of Struts

enter visualbuilder as username and test as password then only it will display the pages of the application.

Example for the Application Managed Security-1The same way we extends the default behaviour of the Struts application, we can validate the user and authorize them tosome resources in the application. We have to just mention the role for the resource in the struts-config.xml file. Thefollowing example will demonstrate the Application managed security. If the user enters the role as admin only then thesubmit will be called otherwise the exception page is displayed on the screen.(1) Struts-config.xml File<?xml version=1.0 encoding=ISO-8859-1 ?><!DOCTYPE struts-config PUBLIC -//Apache Software Foundation//DTD Struts Configuration 1.3//ENhttp://struts.apache.org/dtds/struts-config_1_3.dtd><struts-config><!-- ================================================ Form Bean Definitions --><form-beans><form-bean name=inputForm type=com.arkaaya.InputForm /></form-beans><!-- ========================================= Global Exception Definitions --><global-exceptions></global-exceptions><!-- =========================================== Global Forward Definitions --><global-forwards></global-forwards><!-- =========================================== Action Mapping Definitions --><action-mappings><action path=/submittype=com.arkaaya.SubmitActionscope=request validate=falsename=inputForm input=/index.jsproles=admin/></action-mappings><!-- =========================================== Controller Mapping Definition --><controller contentType=text/html;charset=UTF-8 locale=true debug=1 nocache=trueprocessorClass=org.apache.struts.chain.ComposableRequestProcessor /><!-- ======================================== Message Resources Definitions --><message-resources parameter=MessageResources /></struts-config>(2) chain-config.xml File

Master Of Struts 4/29/2009 12:57 PM

Page 85 of 87

Page 86: Master Of Struts

Example for the Application Managed Security-2(3) CustomCommand.java FileNote:- If any security check application is to be created in the struts 1.3 or higher thenorg.apache.struts.chain.commands.AbstractAuthorizeAction is to be extended but in case of previous versions theRequestProceesor's method processRoles() is to be overridden to check the security setting of the user.package com.arkaaya.command;import org.apache.struts.action.ActionServlet;import org.apache.struts.chain.commands.AbstractAuthorizeAction;import org.apache.struts.chain.contexts.ActionContext;import org.apache.struts.chain.contexts.ServletActionContext;import org.apache.struts.config.ActionConfig;import org.apache.struts.util.MessageResources;import javax.servlet.http.HttpServletRequest;public class CustomCommand extends AbstractAuthorizeAction {// ------------------------------------------------------- Protected Methodsprotected boolean isAuthorized(ActionContext context, String[] roles, ActionConfig mapping) throws Exception {// Identify the HTTP request objectServletActionContext servletActionContext = (ServletActionContext) context;HttpServletRequest request = servletActionContext.getRequest();// Check the current user against the list of required rolesif(request.getParameter(user) != null && request.getParameter(user).equals(admin) ){return (true);}// Default to unauthorizedreturn (false);}protected String getErrorMessage(ActionContext context, ActionConfig actionConfig) {ServletActionContext servletActionContext = (ServletActionContext) context;// Retrieve internal message resourcesActionServlet servlet = servletActionContext.getActionServlet();MessageResources resources = servlet.getInternal();return resources.getMessage(notAuthorized, actionConfig.getPath());}

}

Master Of Struts 4/29/2009 12:57 PM

Page 86 of 87

Page 87: Master Of Struts

*********************I THOUGHT IT WILL USE full 2 U****************************** ************************ *******THANK U************************************ @R.vibhudi @Email [email protected]

Master Of Struts 4/29/2009 12:57 PM

Page 87 of 87