Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Subhin P V
• Struts Frame work is the implementation of
Model-View-Controller (MVC) design pattern
for the JSP.
• Struts is maintained as a part of Apache
• Struts is open source.
What is Model-View-Controller (MVC)
• Model-View-Controller architecture is all
about dividing application components into
three different categories Model, View and
• Components of the MVC architecture has
unique responsibility and each component is
independent of the other component.
• Changes in one component will have no or
less impact on other component.
• Struts is an open source framework used for
developing J2EE web applications using Model
View Controller (MVC) design pattern.
• It uses and extends the Java Servlet API to
encourage developers to adopt an MVC
Struts 2 Core components are Action handler, Result
Handler and Custom Tags.
Action handler interacts with other layers.
Result handler actually dispatches the response to view.
Custom Tags are used render the dynamic content.
The Interceptors are used to specify the
"request-processing lifecycle" for an action.
Interceptors are configured to apply the
common functionalities like workflow,
validation etc.. to the request. Interceptors
code is executed before and after an Action is
How Struts Works
• In struts JavaServerPages (JSP) are used to
design the dynamic web pages.
• In struts, servlets helps to route request which
has been made by the web browsers to the
• The use of servlet as a router helps to make
the web applications easier to design, create,
User Sends request.
FilterDispatcher determines the appropriate
Interceptors are applied.
Execution of Action.
Return of Request(reverse order).
Display the result to user.
Things to download before you start
• Requirements (Downloads)
– JDK ver. 1.5 and above.
– Tomcat 5X and above(Tomcat ver.6 is better)
– Eclipse ver. 3 and above.
– Apache struts2 jar files needed
Example program using struts
We suppose to create a login application using struts.
• Open eclipse and go to fileNewDynamic
project in the new project wizard screen.
• You will have a screen consisting of some
wizards as shown in the next slide.
• After giving the project name click Finish.
• Once you created the project, you can see its
structure in the project folder.
• After these processes, now copy all the jar
files listed in the previous slides to
Class and methods
• Create a class called LoginAction in
• Note that, above action class contains two fields, username and
password which will hold the values from form and also contains an
execute() method that will authenticate the user. In this simple
example, we are checking if username is admin and password is
• Also note that unlike Action class in Struts1, Struts2 action class is a
simple POJO class with required attributes and method.
• The execute() method returns a String value which will determine
the result page. Also, in Struts2 the name of the method is not
fixed. In this example we have define method execute(). You may
want to define a method authenticate() instead.
Create resource file
• Resource bundle is a java entity that helps in
putting the static content away from the
• Here we should define an application file and
should be named as
• To create it the method is on the next slide
• Click that source folder option and you will be
having a following screen.
• And set the source folder as Resources folder.
• In that folder create a file called
• Above programs are made in the folder
Create a struts file
Resourcesresourcescreate file struts.xml
• Copy the following content into struts.xml
Copy the above text into struts.xml.
• Our LoginAction.java class is having a method
called execute(). If the name of the method is
different, e.g. authenticate(). Then we should
specify the name of the method in the action
• You can see <s: submit method command in
the program Login.jsp
Running the application
• Now select Login.jsp then select the run on
• After selecting the command, you will be
having a server lists out of that, select apache-
tomcat server version.6. then run the
• You will be having the following screen on
Enter user_name as admin and password as admin123
If you login successfully, you will get a screen as output.
If you login into the application as a wrong user, the appearing screen will be
like this. You are instructed to renter the user and password again
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Ph: + 91 – 495 40 25 550
Start up Village