Struts 2 tutorial
-
Upload
vinod-pillai -
Category
Technology
-
view
6.015 -
download
1
description
Transcript of Struts 2 tutorial
Presented By: Prof. Vinod Pillai.
(M) 9909023599
http://vinodthebest.wordpress.com
Agenda Part – I : Understanding Struts.
Introduction.
Jakarta Struts.
Struts 2 Architecture.
Part – II : Setting Up Struts.
Getting required Packages.
“Welcome to Struts 2” – Application.
Part – III : Application Development.
10 Applications using Struts 2.
2Prof. Vinod Pillai
Part – IUnderstanding
Struts3Prof. Vinod Pillai
Introduction Java Application = Desktop || Web || Mobile.
Struts = Apache Struts Web Framework.
Framework:
Peace of Software that Automates all tedioustask.
Use Design patterns commonly agreed byIndustry.
In built features that commonly needed by mostof project.
4Prof. Vinod Pillai
Jakarta Struts The Apache Struts web framework is a free open-
source solution for creating Java web applications.
Apache Struts was launched in May 2000, withversion 1.0 officially released in July 2001.
Why we need it?
Web applications based on JSP sometimes combinesdatabase code, page design code, and control flow code.
In practice, we find that unless these concerns areseparated, larger applications become difficult tomaintain.
5Prof. Vinod Pillai
Jakarta Struts One way to separate concerns in a software
application => Model-View-Controller (MVC)architecture.
MVC
Model = Business or database code
View = Page design code
Controller = Navigational code.
Struts framework is designed to help developerscreate web applications that utilize a MVCarchitecture.
6Prof. Vinod Pillai
Model View Controller (MVC)
7Prof. Vinod Pillai
Struts 2 Apache Struts Project offers two major versions:
Struts 1 is recognized as the most popular web applicationframework for Java.
Struts 2 was originally known as WebWork 2.WebWorkand Struts communities joined forces to create Struts 2.
Struts 2 is a pull-MVC framework. i.e. the data that is tobe displayed to user has to be pulled from the Action.
Action class in Struts 2 act as the model in the webapplication.
Unlike Struts, Struts 2 Action class are plain POJOobjects thus simplifying the testing of the code.
8Prof. Vinod Pillai
Struts 2 Struts2 also comes with power APIs to configure
Interceptors that reduce greatly the coupling inapplication.
The view part of Struts 2 is highly configurable and itsupports different result-types such as Velocity,FreeMarker, JSP, etc.
9Prof. Vinod Pillai
Struts 2 Architecture
10Prof. Vinod Pillai
Struts 2 Architecture1. Request is generated by user and sent to Servlet
container.
2. Servlet container invokes FilterDispatcher filter which inturn determines appropriate action.
3. One by one Intercetors are applied before calling theAction. Interceptors performs tasks such as Logging,Validation, File Upload, Double-submit guard etc.
4. Action is executed and the Result is generated byAction.
5. The output of Action is rendered in the view (JSP,Velocity, etc) and the result is returned to the user.
11Prof. Vinod Pillai
Part – II
Setting Up Struts
12Prof. Vinod Pillai
Setting Up Struts*Before we start we need the following tools:
JDK 1.7 (Download)
Eclipse Java EE IDE for Web Developers (Indigo)
Tomcat 7 or any other container (Glassfish, JBoss,Websphere, Weblogic etc) (Download)
Apache Struts-2.0.6 JAR (Download)
MySQL Database.
MySQL Query Browser.
MySQL Java JAR File – Database connectivity.
13Prof. Vinod Pillai
JDK 1.7
http://www.oracle.com/technetwork/java/javase/downloads/index.html 14Prof. Vinod Pillai
Eclipse Java EE IDE
http://eclipse.org/downloads/ 15Prof. Vinod Pillai
Apache Tomcat 7
http://tomcat.apache.org/download-70.cgi 16Prof. Vinod Pillai
Apache Struts 2
http://struts.apache.org/download.cgi 17Prof. Vinod Pillai
MySQL
http://dev.mysql.com/downloads/ 18Prof. Vinod Pillai
Setting Up Struts
JDK 1.7 = .exe
Eclipse Java EE IDE = zip
Tomcat 7 = zip
Apache Struts-2.0.6 JAR = zip
MySQL Database = .exe
Simply Install the software & make sure you remember thepath.
19Prof. Vinod Pillai
Setting Up Struts
20Prof. Vinod Pillai
1“Welcome to Apache Struts 2”
Our Goal :
Our goal is to create a basic Struts2 application with a Submit Button.
User will click on the button and the page will be redirected to a Welcome page which will display message “Welcome to Apache Struts 2”.
21Prof. Vinod Pillai
Getting Started
Start Eclipse and enter the workspace name:
22Prof. Vinod Pillai
Open Eclipse and go to File -> New -> Project and selectDynamic Web Project in the New Project wizard screen.
23Prof. Vinod Pillai
Project Name: Welcome Struts. Target runtime: <Select New Runtime> & show the path where you have unzipped the Tomcat Server. Dynamic web module version: 2.5.
24Prof. Vinod Pillai
Copy JAR files in WebContent -> WEB-INF -> lib folder.
25Prof. Vinod Pillai
The entry point of Struts2 application will be the Filterdefine in deployment descriptor (web.xml). Hence we willdefine an entry oforg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter class in web.xml.
Open web.xml file which is under WEB-INF folder and copypaste following code.
26Prof. Vinod Pillai
web.xml
27Prof. Vinod Pillai
The JSP
We will create two JSP files to render the output to user.
And we need index.jsp will be the starting point of our application which will contain a simple form with Button.
On clicking it will be redirected to Welcome.jsp which will display a simple welcome message.
Create three JSP files [WebContent folder ]:
index.jsp
/WEB-INF/Welcome.jsp
/WEB-INF/Error.jsp
28Prof. Vinod Pillai
index.jsp
29Prof. Vinod Pillai
Welcome.jsp
Error.jsp
30Prof. Vinod Pillai
struts.xml
Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loaded from the classpath of the project. We will define struts.xml file in the source/src folder.
31Prof. Vinod Pillai
The Action Class
We will need an Action class that will accept the requestand return success. For this we will create a packagecom.vinod.struts in the source folder/src. This package willcontain the action file.
32Prof. Vinod Pillai
Run the Application:
To run the project, right click on Project name from Project Explorer and select Run as -> Run on Server.
Note:
In Eclipse if you run the Web Application it will run inside the Eclipse inside Web Browser if you would like to change it to system installed browser then:
Select Window -> Web Browser -> Firefox.
33Prof. Vinod Pillai
Part – III
Application Development
34Prof. Vinod Pillai
2“Action & Form – Name & Last name –
Display.”Our Goal :
To create a Struts2 application with a index page. User will enter his First Name & Last Name and it will be redirected to a Welcome page which will display <First Name> & <Last Name>.
35Prof. Vinod Pillai
Final Structure:
36Prof. Vinod Pillai
index.jsp
37Prof. Vinod Pillai
struts.xml
38Prof. Vinod Pillai
Action Class
39Prof. Vinod Pillai
Welcome.jsp
40Prof. Vinod Pillai
Note: [Most Important Flow]
Request is generated by user and sent to Servlet container.
Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.
One by one Intercetors are applied before calling the Action.
Action is executed and the Result is generated by Action.
The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user.
41Prof. Vinod Pillai
3“Action Class (POJO/Plain Old Java Object)
& Form”Our Goal :
To create a Struts2 application with an index page. User willenter his First Name, Last Name and Address and it will beredirected to a Welcome page which will display <Name><Last Name> & <Address>.
Same as the above application only change is here thatAction Class is a POJO Class.
Other all same only change in Action Class.
42Prof. Vinod Pillai
Final Structure:
43Prof. Vinod Pillai
Action Class:
44Prof. Vinod Pillai
Note:
Here the action class doesn't extend any other class orinterface. The method invoked in the processing of an actionis the "execute" method.
How this happened?
All actions may implement this interface, which exposes theexecute() method.
You are free to create POJOs that maintains the samecontract defined by this interface without actuallyimplementing the interface.
45Prof. Vinod Pillai
Note:
46Prof. Vinod Pillai
Note:
47Prof. Vinod Pillai
Interceptors Interceptors are one of the most powerful features
of Struts 2.
As Interceptor, as the name suggests, intercepts therequest, and provides some additional processingbefore and after the execution of action and result.
Common functionalities required by each action areimplemented as Interceptors.
These functionalities may include validation ofinput data, pre-processing of file upload, andsometimes pre-processing of controls with somedata before the web page appears, etc.
48Prof. Vinod Pillai
Interceptors The interceptor approach helps in modularizing
common code into reusable classes.
The framework provides a set of Interceptors, whichcan be used to provide the required functionalitiesto an action.
User can create his custom interceptors, to get thefunctionality required for each action.
49Prof. Vinod Pillai
Interceptors Observe that in the figure the Action class is
executed, then a request passes through the set ofInterceptors which provide request pre-processingwhich is required before the action is executed.
Similarly after the execution of Action class allInterceptors are executed once more to providepost-processing if any.
But this time the execution order of Interceptors isreversed.
50Prof. Vinod Pillai
Interceptors Creating Custom Interceptor : [Two Ways]
Implementing Interceptor Interface.
Extending AbstractInterceptor Class.
Configuring Interceptors:
51Prof. Vinod Pillai
4“Interceptors”
Our Goal :
To create a Struts2 application with an index page. Userclicks the submit button before passing the request toAction it will be send to Interceptor then it will go to Actionand finally before showing the result it will send toInterceptor and then to Welcome.jsp/result.
52Prof. Vinod Pillai
Final Structure:
53Prof. Vinod Pillai
Interceptor Class:
54Prof. Vinod Pillai
Struts.xml:
55Prof. Vinod Pillai
Output:
56Prof. Vinod Pillai
Note:
With most web applications, we find ourselves wanting toapply the same set of Interceptors over and over again.Rather than reiterate the same list of Interceptors, we canbundle these Interceptors together using an InterceptorStack.
57Prof. Vinod Pillai
5“Common Action Class – Multiple Execute
Method ”Our Goal :
To create a Struts2 application with an index page. When theuser clicks the Update or Insert Button both goes to sameAction Class but different execute for each button.
58Prof. Vinod Pillai
Final Structure:
59Prof. Vinod Pillai
Index.jsp:
60Prof. Vinod Pillai
Struts.xml:
61Prof. Vinod Pillai
Action Class :
62Prof. Vinod Pillai
Insert.jsp:
63Prof. Vinod Pillai
Update.jsp:
64Prof. Vinod Pillai
Output:
65Prof. Vinod Pillai
Output:
66Prof. Vinod Pillai
6“Validation – Using validate () Method & XML”
Our Goal :
To create a Struts2 application with an index page. If userclicks the Login Button without entering the Username orPassword then it should display message appropriately errormessage.
67Prof. Vinod Pillai
Final Structure:
68Prof. Vinod Pillai
Index.jsp:
69Prof. Vinod Pillai
Struts.xml:
70Prof. Vinod Pillai
Action Class:
71Prof. Vinod Pillai
Welcome.jsp:
72Prof. Vinod Pillai
Second Option : XML File Validation
73Prof. Vinod Pillai
Final Structure:
74Prof. Vinod Pillai
LoginAction-validation.xml :
75Prof. Vinod Pillai
Action Class :
76Prof. Vinod Pillai
7“User Form”
Our Goal :
To create a Struts2 application with an index page. If userclicks the Login Button it will redirect him to Welcome pagewhere some field values are coming from Action Classvariable & some major Form Fields.
77Prof. Vinod Pillai
Action Class :
78Prof. Vinod Pillai
Welcome.jsp:
79Prof. Vinod Pillai
8“Integrating Tiles Plug-in”
Our Goal :
To create a Struts2 application with an index page. As userclicks the Login Button it will redirect him to Welcome pagewhich having a common design layout which will becommon for all the pages.
80Prof. Vinod Pillai
Final Structure:
81Prof. Vinod Pillai
Index.jsp:
82Prof. Vinod Pillai
Struts.xml:
83Prof. Vinod Pillai
tiles.xml:
84Prof. Vinod Pillai
MainLayout.jsp:
85Prof. Vinod Pillai
Header.jsp:
Menu.jsp:
86Prof. Vinod Pillai
FirstPage.jsp:
SecondPage.jsp:
87Prof. Vinod Pillai
8 & 9“HttpServletRequest, HttpServletResponse, ServletContext
& HttpSession”Our Goal :
Getting ServletContext Object.
Getting HttpServletRequest Object.
Getting HttpServletResponse Object.
Getting HttpSession Object.
88Prof. Vinod Pillai
ApplicationAware/ServletContext:
89Prof. Vinod Pillai
SessionAware/HttpSession:
90Prof. Vinod Pillai
ServletRequestAware/HttpServletRequest:
91Prof. Vinod Pillai
ServletResponseAware :
92Prof. Vinod Pillai
10“Database – Insert + Validation + View + Update”
Our Goal :
To create a Struts2 Final application in we will provide usertwo options:
First : Allow to create a New Account and the data willentered to Database.
Second : Allow user to login into the system and view hisdetails + Update his details.
Major features:
Database connectivity.
HttpServletRequest.
Session.93Prof. Vinod Pillai
Database Output:
94Prof. Vinod Pillai
Final Structure :
95Prof. Vinod Pillai
Index.jsp :
96Prof. Vinod Pillai
Struts.xml :
97Prof. Vinod Pillai
Login Action :
98Prof. Vinod Pillai
Login Action :
99Prof. Vinod Pillai
Login Action :
100Prof. Vinod Pillai
UserClass.java / POJO /Bean:
101Prof. Vinod Pillai
EditUser.jsp :
102Prof. Vinod Pillai
UpdateUserAction.java :
103Prof. Vinod Pillai
UpdateUserAction.java :
104Prof. Vinod Pillai
Conclusion:
105Prof. Vinod Pillai
What is Framework? & MVC.
Struts Architecture.
Setting Up Struts.
Welcome Struts 2 – Application.
Action – POJO.
Multiple Execute method in Action Class.
Major Form Fields.
Validation using validate() & .xml File.
Tiles Plug-in.
Request & Session Object.
Database.
Real Struts 2 Architecture
106Prof. Vinod Pillai