vinod sir struts 2 part 2

56
Prof. Vinod Pillai. [email protected] http://vinodthebest.wordpress.com www.youtube.com/vinodthebest

Upload: vinod-pillai

Post on 07-Nov-2014

6.246 views

Category:

Education


3 download

DESCRIPTION

Configuring Struts 2 in Eclipse, Action Class and Interceptors.

TRANSCRIPT

Page 1: Vinod sir struts 2   part 2

Prof. Vinod Pillai.

[email protected]

http://vinodthebest.wordpress.com

www.youtube.com/vinodthebest

Page 2: Vinod sir struts 2   part 2

Part – II

Setting Up Struts

2Prof. Vinod Pillai

Page 3: Vinod sir struts 2   part 2

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

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

Apache Struts 2

http://struts.apache.org/download.cgi 5Prof. Vinod Pillai

Page 6: Vinod sir struts 2   part 2

MySQL

http://dev.mysql.com/downloads/ 6Prof. Vinod Pillai

Page 7: Vinod sir struts 2   part 2

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

Setting Up Struts

8Prof. Vinod Pillai

Page 9: Vinod sir struts 2   part 2

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

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

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

Copy JAR files in WebContent -> WEB-INF -> lib folder.

12Prof. Vinod Pillai

Page 13: Vinod sir struts 2   part 2

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

web.xml

14Prof. Vinod Pillai

Page 15: Vinod sir struts 2   part 2

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

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

16Prof. Vinod Pillai

Page 17: Vinod sir struts 2   part 2

Welcome.jsp

Error.jsp

17Prof. Vinod Pillai

Page 18: Vinod sir struts 2   part 2

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

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

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

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

Final Structure:

22Prof. Vinod Pillai

Page 23: Vinod sir struts 2   part 2

index.jsp

23Prof. Vinod Pillai

Page 24: Vinod sir struts 2   part 2

struts.xml

24Prof. Vinod Pillai

Page 25: Vinod sir struts 2   part 2

Action Class

25Prof. Vinod Pillai

Page 26: Vinod sir struts 2   part 2

Welcome.jsp

26Prof. Vinod Pillai

Page 27: Vinod sir struts 2   part 2

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

Final Structure:

28Prof. Vinod Pillai

Page 29: Vinod sir struts 2   part 2

Action Class:

29Prof. Vinod Pillai

Page 30: Vinod sir struts 2   part 2

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

Note:

31Prof. Vinod Pillai

Page 32: Vinod sir struts 2   part 2

Note:

32Prof. Vinod Pillai

Page 33: Vinod sir struts 2   part 2

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

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

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

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

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

Final Structure:

38Prof. Vinod Pillai

Page 39: Vinod sir struts 2   part 2

Interceptor Class:

39Prof. Vinod Pillai

Page 40: Vinod sir struts 2   part 2

Struts.xml:

40Prof. Vinod Pillai

Page 41: Vinod sir struts 2   part 2

Output:

41Prof. Vinod Pillai

Page 42: Vinod sir struts 2   part 2

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

Interceptor Class: [Getting Form Value & making changes]

43Prof. Vinod Pillai

Page 44: Vinod sir struts 2   part 2

Output :

44Prof. Vinod Pillai

Page 45: Vinod sir struts 2   part 2

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

Final Structure:

46Prof. Vinod Pillai

Page 47: Vinod sir struts 2   part 2

Index.jsp:

47Prof. Vinod Pillai

Page 48: Vinod sir struts 2   part 2

Struts.xml:

48Prof. Vinod Pillai

Page 49: Vinod sir struts 2   part 2

Action Class :

49Prof. Vinod Pillai

Page 50: Vinod sir struts 2   part 2

Insert.jsp:

50Prof. Vinod Pillai

Page 51: Vinod sir struts 2   part 2

Update.jsp:

51Prof. Vinod Pillai

Page 52: Vinod sir struts 2   part 2

Output:

52Prof. Vinod Pillai

Page 53: Vinod sir struts 2   part 2

Output:

53Prof. Vinod Pillai

Page 54: Vinod sir struts 2   part 2

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

Part – II

Completed

55Prof. Vinod Pillai

Page 56: Vinod sir struts 2   part 2

Thank You

[email protected]

56Prof. Vinod Pillai