vinod sir struts 2 part 2
DESCRIPTION
Configuring Struts 2 in Eclipse, Action Class and Interceptors.TRANSCRIPT
Prof. Vinod Pillai.
http://vinodthebest.wordpress.com
www.youtube.com/vinodthebest
Part – II
Setting Up Struts
2Prof. Vinod Pillai
Part – II : Setting Up Struts & Running basic App.
Setting Up Struts 2 & Running Hello World Application.
Action Class & Form – Name & Last name – Display.
Action Class (POJO/Plain Old Java Object) & Form.
Interceptors.
Common Action Class – Multiple Execute Method .
Lunch Break: 01:45 p.m. to 02:00 p.m.
Prof. Vinod Pillai 3
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.
4Prof. Vinod Pillai
Apache Struts 2
http://struts.apache.org/download.cgi 5Prof. Vinod Pillai
MySQL
http://dev.mysql.com/downloads/ 6Prof. 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.
7Prof. Vinod Pillai
Setting Up Struts
8Prof. 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”.
9Prof. Vinod Pillai
Open Eclipse and go to File -> New -> Project and selectDynamic Web Project in the New Project wizard screen.
10Prof. 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.
11Prof. Vinod Pillai
Copy JAR files in WebContent -> WEB-INF -> lib folder.
12Prof. 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.
13Prof. Vinod Pillai
web.xml
14Prof. 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
15Prof. Vinod Pillai
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
16Prof. Vinod Pillai
Welcome.jsp
Error.jsp
17Prof. 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.
18Prof. 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.
19Prof. 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.
20Prof. 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>.
21Prof. Vinod Pillai
Final Structure:
22Prof. Vinod Pillai
index.jsp
23Prof. Vinod Pillai
struts.xml
24Prof. Vinod Pillai
Action Class
25Prof. Vinod Pillai
Welcome.jsp
26Prof. 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.
27Prof. Vinod Pillai
Final Structure:
28Prof. Vinod Pillai
Action Class:
29Prof. 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.
Try : [ActionSupport , Action & POJO ]
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.
30Prof. Vinod Pillai
“Important Points:”
Note:
31Prof. Vinod Pillai
Note:
32Prof. 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.
33Prof. 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.
34Prof. Vinod Pillai
Interceptors Observe that in the figure the before Action class is
executed, request passes through the set ofInterceptors which provide request pre-processing.
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.
35Prof. Vinod Pillai
Interceptors Creating Custom Interceptor : [Two Ways]
Implementing Interceptor Interface.
Extending AbstractInterceptor Class.
Configuring Interceptors:
36Prof. 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.
37Prof. Vinod Pillai
Final Structure:
38Prof. Vinod Pillai
Interceptor Class:
39Prof. Vinod Pillai
Struts.xml:
40Prof. Vinod Pillai
Output:
41Prof. 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.
42Prof. Vinod Pillai
Interceptor Class: [Getting Form Value & making changes]
43Prof. Vinod Pillai
Output :
44Prof. 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.
45Prof. Vinod Pillai
Final Structure:
46Prof. Vinod Pillai
Index.jsp:
47Prof. Vinod Pillai
Struts.xml:
48Prof. Vinod Pillai
Action Class :
49Prof. Vinod Pillai
Insert.jsp:
50Prof. Vinod Pillai
Update.jsp:
51Prof. Vinod Pillai
Output:
52Prof. Vinod Pillai
Output:
53Prof. Vinod Pillai
Part – II : Setting Up Struts & Running basic App.
Setting Up Struts 2 & Running Hello World Application.
Action Class & Form – Name & Last name – Display.
Action Class (POJO/Plain Old Java Object) & Form.
Interceptors.
Common Action Class – Multiple Execute Method .
Lunch Break: 01:45 p.m. to 02:00 p.m.
Prof. Vinod Pillai 54
Part – II
Completed
55Prof. Vinod Pillai