Professional Documents
Culture Documents
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
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.
Then Eclipse will ask you for name of the project. Enter the name of the project and click on
finish.
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
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
<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.
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 :
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
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 :
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
:
http://www.raistudies.com/struts-tutorials/