vinod sir struts 2 part 2
DESCRIPTION
Configuring Struts 2 in Eclipse, Action Class and Interceptors.TRANSCRIPT
![Page 1: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/1.jpg)
Prof. Vinod Pillai.
http://vinodthebest.wordpress.com
www.youtube.com/vinodthebest
![Page 2: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/2.jpg)
Part – II
Setting Up Struts
2Prof. Vinod Pillai
![Page 3: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/3.jpg)
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
![Page 4: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/4.jpg)
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
![Page 5: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/5.jpg)
Apache Struts 2
http://struts.apache.org/download.cgi 5Prof. Vinod Pillai
![Page 6: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/6.jpg)
MySQL
http://dev.mysql.com/downloads/ 6Prof. Vinod Pillai
![Page 7: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/7.jpg)
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
![Page 8: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/8.jpg)
Setting Up Struts
8Prof. Vinod Pillai
![Page 9: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/9.jpg)
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
![Page 10: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/10.jpg)
Open Eclipse and go to File -> New -> Project and selectDynamic Web Project in the New Project wizard screen.
10Prof. Vinod Pillai
![Page 11: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/11.jpg)
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
![Page 12: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/12.jpg)
Copy JAR files in WebContent -> WEB-INF -> lib folder.
12Prof. Vinod Pillai
![Page 13: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/13.jpg)
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
![Page 14: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/14.jpg)
web.xml
14Prof. Vinod Pillai
![Page 15: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/15.jpg)
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
![Page 16: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/16.jpg)
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
16Prof. Vinod Pillai
![Page 17: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/17.jpg)
Welcome.jsp
Error.jsp
17Prof. Vinod Pillai
![Page 18: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/18.jpg)
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
![Page 19: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/19.jpg)
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
![Page 20: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/20.jpg)
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
![Page 21: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/21.jpg)
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
![Page 22: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/22.jpg)
Final Structure:
22Prof. Vinod Pillai
![Page 23: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/23.jpg)
index.jsp
23Prof. Vinod Pillai
![Page 24: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/24.jpg)
struts.xml
24Prof. Vinod Pillai
![Page 25: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/25.jpg)
Action Class
25Prof. Vinod Pillai
![Page 26: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/26.jpg)
Welcome.jsp
26Prof. Vinod Pillai
![Page 27: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/27.jpg)
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
![Page 28: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/28.jpg)
Final Structure:
28Prof. Vinod Pillai
![Page 29: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/29.jpg)
Action Class:
29Prof. Vinod Pillai
![Page 30: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/30.jpg)
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:”
![Page 31: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/31.jpg)
Note:
31Prof. Vinod Pillai
![Page 32: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/32.jpg)
Note:
32Prof. Vinod Pillai
![Page 33: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/33.jpg)
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
![Page 34: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/34.jpg)
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
![Page 35: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/35.jpg)
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
![Page 36: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/36.jpg)
Interceptors Creating Custom Interceptor : [Two Ways]
Implementing Interceptor Interface.
Extending AbstractInterceptor Class.
Configuring Interceptors:
36Prof. Vinod Pillai
![Page 37: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/37.jpg)
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
![Page 38: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/38.jpg)
Final Structure:
38Prof. Vinod Pillai
![Page 39: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/39.jpg)
Interceptor Class:
39Prof. Vinod Pillai
![Page 40: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/40.jpg)
Struts.xml:
40Prof. Vinod Pillai
![Page 41: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/41.jpg)
Output:
41Prof. Vinod Pillai
![Page 42: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/42.jpg)
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
![Page 43: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/43.jpg)
Interceptor Class: [Getting Form Value & making changes]
43Prof. Vinod Pillai
![Page 44: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/44.jpg)
Output :
44Prof. Vinod Pillai
![Page 45: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/45.jpg)
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
![Page 46: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/46.jpg)
Final Structure:
46Prof. Vinod Pillai
![Page 47: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/47.jpg)
Index.jsp:
47Prof. Vinod Pillai
![Page 48: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/48.jpg)
Struts.xml:
48Prof. Vinod Pillai
![Page 49: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/49.jpg)
Action Class :
49Prof. Vinod Pillai
![Page 50: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/50.jpg)
Insert.jsp:
50Prof. Vinod Pillai
![Page 51: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/51.jpg)
Update.jsp:
51Prof. Vinod Pillai
![Page 52: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/52.jpg)
Output:
52Prof. Vinod Pillai
![Page 53: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/53.jpg)
Output:
53Prof. Vinod Pillai
![Page 54: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/54.jpg)
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
![Page 55: Vinod sir struts 2 part 2](https://reader034.vdocument.in/reader034/viewer/2022051322/545caddfb1af9f3c0a8b482a/html5/thumbnails/55.jpg)
Part – II
Completed
55Prof. Vinod Pillai