You are on page 1of 13

Architecture of Struts 1 MVC Framework

In this post we will see the architecture of Struts 1 or work flow of popular mvc framework
that is Struts 1. As we know struts 1 is a mvc (Model-View-Controller) based framework
which reduces the work of developers to develop a web application. So, one question will
come in your mind i.e. What are the elements in Struts 1 that serves as Model-ViewController rolls ?
Here is the answer :
Model : Models in Struts 1 framework are the java bean classes that extends ActionForm
class provided in Struts jars. Model classes holds the state of the internal and external
system.
View : Views are web components that are shown to the web browser users. In Struts 1, it
can be jsp page or any view render technique such as Tiles.
Controller : Controllers are the components that handles are the requests that comes from
users and decides which view is to send back to the use as response. In Struts 1 the
controller part is handled by ActionServlet class and a subclass of Action class that is
developer defined. The controller may interact with the application specific business logic or
some code that persists data in database or with an EJB.
The bellow figure shows the work flow of Struts 1 :

Architecture of Struts 1

The following steps shows the work flow of Strust 1 framework :

1. When first time user request comes from the browser, ActionServlet invoked which reads
struts-config.xml configuration file and creates configuration objects. struts-config.xml
contains information about the FormBeans, that are subclasses of ActionForm, Actions and
view that can be send as response to the browser.
2. In the second step ActionServlet instantiate the specified form bean and puts the value of
the form attributes, submitted by the browser, to form bean.
3. Then ActionServlet calls the Action classs execute method, which returns ActionForward
instance. ActionForward instance contains the detail of the view to be return as response.
4. ActionServlet renders the view and return to the browser as response.

Hello World with Struts 1 in Eclipse IDE


In the previous post we saw the architecture of Struts 1. Now this post will take you through
the first illustrative program made up with the help of Struts 1 framework to display Hello
World in browser in Eclipse IDE. I have used Eclipse 3.7 and Tomcat 6 for developing the
example set.
First of all click on New Project and the Dynamic Web Project.

Select Dynamic Web Project

Then Eclipse will ask you for name of the project. Enter the name of the project and click on
finish.

Provide Project Name

This will create a web project in Eclipse which can be exported as war file and run on Tomcat
6 server.
The next step will be to add struts jar files. The following jar files should be added to the
project for successful deployment of struts project :

antlr-2.7.2.jar

commons-beanutils-1.8.0.jar

commons-chain-1.2.jar

commons-digester-1.8.jar

commons-logging-1.0.4.jar

commons-validator-1.3.1.jar

oro-2.0.8.jar

struts-core-1.3.10.jar

struts-taglib-1.3.10.jar
These jar files can be downloaded from the link with the Struts 1.3 distribution.You can put
these jar files to the directory under WebContent -> WEB-INF -> lib.
Now we have to configure ActionServlet of struts with web.xml. The following xml shows how
to configure struts in web.xml.
1
2
3
4
5

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


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
6
version="2.5">
7
<display-name>HelloWordWithStruts1</display-name>
8
9
<serlet>
10
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet11
class>
12
<init-param>
13
<param-name>config</param-name>
14
<param-value>
15
/WEB-INF/struts-config.xml
16
</param-value>
17
</init-param>
18
<load-on-startup>2</load-on-startup>
19
</servlet>
20
21
<servlet-mapping>
22
<servlet-name>action</servlet-name>
23
<url-pattern>*.do</url-pattern>
24
</servlet-mapping>
25
26
<welcome-file-list>
27
<welcome-file>index.jsp</welcome-file>

28
</welcome-file-list>
29
</web-app>
We have registered ActionServlet class in web.xml for the url *.do , that means every url
that ends with .do will be taken care by Struts. We have to also specify location of strutsconfig.xml as init-param with name config. Struts reads the file strutsconfig.xml about the configuration that we provide to the struts such as FormBeans,
Actions, Plugins , Global Forwards etc.
After that we have to create a form bean that will be sub class of ActionForm class which will
bw act as model in out application and will contain application state. Following is our form
bean named HelloWorldForm.
1
package com.raistudies.forms;
2
3 import org.apache.struts.action.ActionForm;
4
5 public class HelloWorldForm extends ActionForm {
6
7
private static final long serialVersionUID = 7352021000623040587L;
8
9
private String hello = null;
10
11
public HelloWorldForm() {
12
super();
13
hello = "Hello World";
14
}
15
16
public String getHello() {
17
return hello;
18
}
19
20
public void setHello(String hello) {
21
this.hello = hello;
22
}
23
}
This form bean contains single property named as hello which stores Hello World as
default value. Our next step will be to create a controller helper class that will help to handle
a particulate url pattern. The url pattern will be specified and associated with our controller
helper class in the file struts-config.xml. Our controller helper class must be a subclass of
Action class provided in struts jar. We the url will be requested to the server, struts will
run execute method of the associated with the url and returns the view in response.
Following is our controller helper class named as HelloWorldAction :
1
package com.raistudies.actions;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import org.apache.struts.action.Action;

7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9
import org.apache.struts.action.ActionMapping;
10
11 public class HelloWorldAction extends Action {
12
13
@Override
14
public ActionForward execute(ActionMapping mapping, ActionForm form,
15
HttpServletRequest request, HttpServletResponse response)
16
throws Exception {
17
return mapping.findForward("hello");
18
}
19
20
}
The ActionForm object will contain instance of our form bean i.e. HelloWorldForm. The
execute method returns string hello as the ActionForward. All the configuration regarding
the action will be made as in the file struts-config.xml mentioned bellow.
1
2
3
4
5
6
7

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


<!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 ==================================
8
-->
9
<form-beans>
<form-bean name="HelloForm"
10
type="com.raistudies.forms.HelloWorldForm"/>
11
</form-beans>
<!-- ========== Action Mapping Definitions
12
============================= -->
13
<action-mappings>
<action name="HelloForm" path="/HelloWorld"
14
type="com.raistudies.actions.HelloWorldAction" scope="request" >
15
<forward name="hello" path="/HelloWorld.jsp"/>
16
</action>
17
</action-mappings>
18
</struts-config>
As you can see, we have registered our form bean com.raistudies.forms.HelloWorldForm with
name HelloForm and put our controller helper class
com.raistudies.actions.HelloWorldAction with the following attributes :
1.

name = HelloForm : It specifies the name of the form bean to be use as


Model and in this case it is com.raistudies.forms.HelloWorldForm.
2.
path=/HelloWorld : It indicate the url associated to the controller helper
class. In this case whene we will hit the url /HelloWorld.do , action will be invoked.

3.

scope=request : It indicates that the model or the form bean HelloForm will
be stored in HttpServletRequest instance. the other option can be session.
<forward name=hello path=/HelloWorld.jsp redirect=true/> indicated a view as
HelloWorld.jsp that is named as hello. That means if the execute method returns
ActionForward with value hello, server will send HelloWorld.jsp as response.
So the directory structure of the project will be like bellow figure :

Directory Structure

When we will run the project in Tomcat 6 bellow output window will be the output :

Login Form Example With Struts


This post will show you how a form process in struts 1 framework. We will create a login form
and then using struts 1 we will verify the authentication of the user. This example will take
following steps :
1.

First of all we will create a form bean (LoginForm.java) that will hold the form
values provided by the user.
2.
Create a jsp page (Login.jsp) which will contain the form to be displayed to the
user.
3.
Create a success page (Success.jsp) and failure page (Failure.jsp) for providing
feedback to the user on their form submission.
4.
Create a controller helper class (LoginAction.java) that will check for the user
input and decide which view to be respond to the users (Success.jsp or Failure.jsp).
5.
And finally we will configure our form bean and action classes in strutsconfig.xml.
LoginForm.java
Following is the code in LoginForm.java file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14

package com.raistudies.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm {
private static final long serialVersionUID = -3491637470205228033L;
private String username = null;
private String password = null;

15
public String getUsername() {
16
return username;
17
}
18
19
public void setUsername(String username) {
20
this.username = username;
21
}
22
23
public String getPassword() {
24
return password;
25
}
26
27
public void setPassword(String password) {
28
this.password = password;
29
}
30
31
@Override
32
public void reset(ActionMapping mapping, HttpServletRequest request) {
33
this.password = null;
34
}
35
}
There are two fields in this form bean username and password, that will hold the value
of two fields in the login form. One new goods here is the method reset which is
overwritten by our form bean. reset method is called at the end of the every request
processed by the struts. In reset method we have set the value of password as null which
means every time the user will open the login jsp in browser it will show the last username
but will not show the value of password.
Login.jsp
Bellow are the content of Login.jsp :
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2
pageEncoding="ISO-8859-1"%>
3 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
4
"http://www.w3.org/TR/html4/loose.dtd">
5
<html>
6
<head>
7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8 <title>Login Form</title>
9
</head>
10
<body>
11
<html:form action="/Login" focus="username">
12
Username : <html:text property="username"/><br/>
13
Password : <html:password property="password"/><br/>
14
<html:submit value="Login"/>
15
</html:form>
16
</body>
17
</html>

This jsp will render a form with two fields, username and password. Struts HTML taglib
has been used to create the form. <html:form action=/Login focus=username> will
render as a html form with submit url as /Login.do, so our action must use this path in
configuration to be run. <html:text property=username/> will be render as html input tag
and its value will be put in the username field of the form bean LoginForm. Same with the
property password.
LoginAction.java
Lets look inside the code in LoginAction.java :
1
2
3
4
5
6

public class LoginAction extends Action {


@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm loginForm = (LoginForm)form;
if(loginForm.getUsername() == null || loginForm.getPassword() ==
7
null ||
!loginForm.getUsername().equalsIgnoreCase("rahul") || !
8
loginForm.getPassword().equals("abc")){
9
return mapping.findForward("failure");
10
}
11
else
12
return mapping.findForward("success");
13
}
14
}
As you can see first of all the ActionForm instance is typecast to LoginForm in the execute
method of LoginAction and then logic to verify the username and password will decide which
view to be send back to the user. In this case, username must be rahul ans password must
be abc to go to the view associated with the success, otherwise view associated with
failure will be returned. We can see the jsp files associated with success and failure.
struts-config.xml
Entries of form beans and actions aer done in struts-config.xml as follows :
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts-config PUBLIC
3
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
4
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
5 <struts-config>
<!-- ========== Form Bean Definitions ==================================
6
-->
7
<form-beans>
8
<form-bean name="loginForm" type="com.raistudies.forms.LoginForm"/>
9
</form-beans>
<!-- ========== Action Mapping Definitions
10
============================= -->
11
<action-mappings>
12
<action name="loginForm" path="/Login"
type="com.raistudies.actions.LoginAction" scope="request"

input="/Login.jsp">
<forward name="failure" path="/Failure.jsp"
13
redirect="true"/>
<forward name="success" path="/Success.jsp"
14
redirect="true"/>
15
</action>
16
</action-mappings>
17
</struts-config>
Our LoginForm class has been added as a form bean named loginForm and associated with
the action class LoginAction. Attributes associated with the action class are specified as with
the following reason :
1.

name=loginForm : Struts will instantiate the LoginForm class will set the
value of form properties.
2.
path=/Login : The action class will be associated with the request path
/Login.do. Hence the Login.jsp contains /Login as action attribute value.
3.
input=/Login.jsp : Form inputs will be taken from Login.jsp.
There are two action forwards also registered with our action which tells struts that if
failure is return by action then the response view will be /Failure.jsp and if success is
returned by action then the response view will be /Success.jsp.
Deploy the war file in Tomcat 6 and hit the url in your browser you will get the following login
form :

Now, put Rahul as username and abc as password you will be carried out to the
Success.jsp page :

Login Successful Form

Now, try with the username as scote and tiger. It will show you Failure.jsp page :

Login Failure

If you import the code provided in bellow link in Eclipse. The project directory will be like this
:

Project Hierarchi in Eclipse

http://www.raistudies.com/struts-tutorials/

You might also like