Struts 2 tutorial

107
Presented By: Prof. Vinod Pillai. (M) 9909023599 [email protected] http://vinodthebest.wordpress.com

description

Struts 2 Tutorial with Examples.

Transcript of Struts 2 tutorial

Page 1: Struts 2 tutorial

Presented By: Prof. Vinod Pillai.

(M) 9909023599

[email protected]

http://vinodthebest.wordpress.com

Page 2: Struts 2 tutorial

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

Page 3: Struts 2 tutorial

Part – IUnderstanding

Struts3Prof. Vinod Pillai

Page 4: Struts 2 tutorial

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

Page 5: Struts 2 tutorial

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

Page 6: Struts 2 tutorial

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

Page 7: Struts 2 tutorial

Model View Controller (MVC)

7Prof. Vinod Pillai

Page 8: Struts 2 tutorial

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

Page 9: Struts 2 tutorial

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

Page 10: Struts 2 tutorial

Struts 2 Architecture

10Prof. Vinod Pillai

Page 11: Struts 2 tutorial

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

Page 12: Struts 2 tutorial

Part – II

Setting Up Struts

12Prof. Vinod Pillai

Page 13: Struts 2 tutorial

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

Page 14: Struts 2 tutorial

JDK 1.7

http://www.oracle.com/technetwork/java/javase/downloads/index.html 14Prof. Vinod Pillai

Page 15: Struts 2 tutorial

Eclipse Java EE IDE

http://eclipse.org/downloads/ 15Prof. Vinod Pillai

Page 16: Struts 2 tutorial

Apache Tomcat 7

http://tomcat.apache.org/download-70.cgi 16Prof. Vinod Pillai

Page 17: Struts 2 tutorial

Apache Struts 2

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

Page 18: Struts 2 tutorial

MySQL

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

Page 19: Struts 2 tutorial

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

Page 20: Struts 2 tutorial

Setting Up Struts

20Prof. Vinod Pillai

Page 21: Struts 2 tutorial

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

Page 22: Struts 2 tutorial

Getting Started

Start Eclipse and enter the workspace name:

22Prof. Vinod Pillai

Page 23: Struts 2 tutorial

Open Eclipse and go to File -> New -> Project and selectDynamic Web Project in the New Project wizard screen.

23Prof. Vinod Pillai

Page 24: Struts 2 tutorial

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

Page 25: Struts 2 tutorial

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

25Prof. Vinod Pillai

Page 26: Struts 2 tutorial

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

Page 27: Struts 2 tutorial

web.xml

27Prof. Vinod Pillai

Page 28: Struts 2 tutorial

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

Page 29: Struts 2 tutorial

index.jsp

29Prof. Vinod Pillai

Page 30: Struts 2 tutorial

Welcome.jsp

Error.jsp

30Prof. Vinod Pillai

Page 31: Struts 2 tutorial

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

Page 32: Struts 2 tutorial

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

Page 33: Struts 2 tutorial

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

Page 34: Struts 2 tutorial

Part – III

Application Development

34Prof. Vinod Pillai

Page 35: Struts 2 tutorial

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

Page 36: Struts 2 tutorial

Final Structure:

36Prof. Vinod Pillai

Page 37: Struts 2 tutorial

index.jsp

37Prof. Vinod Pillai

Page 38: Struts 2 tutorial

struts.xml

38Prof. Vinod Pillai

Page 39: Struts 2 tutorial

Action Class

39Prof. Vinod Pillai

Page 40: Struts 2 tutorial

Welcome.jsp

40Prof. Vinod Pillai

Page 41: Struts 2 tutorial

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

Page 42: Struts 2 tutorial

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

Page 43: Struts 2 tutorial

Final Structure:

43Prof. Vinod Pillai

Page 44: Struts 2 tutorial

Action Class:

44Prof. Vinod Pillai

Page 45: Struts 2 tutorial

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

Page 46: Struts 2 tutorial

Note:

46Prof. Vinod Pillai

Page 47: Struts 2 tutorial

Note:

47Prof. Vinod Pillai

Page 48: Struts 2 tutorial

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

Page 49: Struts 2 tutorial

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

Page 50: Struts 2 tutorial

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

Page 51: Struts 2 tutorial

Interceptors Creating Custom Interceptor : [Two Ways]

Implementing Interceptor Interface.

Extending AbstractInterceptor Class.

Configuring Interceptors:

51Prof. Vinod Pillai

Page 52: Struts 2 tutorial

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

Page 53: Struts 2 tutorial

Final Structure:

53Prof. Vinod Pillai

Page 54: Struts 2 tutorial

Interceptor Class:

54Prof. Vinod Pillai

Page 55: Struts 2 tutorial

Struts.xml:

55Prof. Vinod Pillai

Page 56: Struts 2 tutorial

Output:

56Prof. Vinod Pillai

Page 57: Struts 2 tutorial

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

Page 58: Struts 2 tutorial

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

Page 59: Struts 2 tutorial

Final Structure:

59Prof. Vinod Pillai

Page 60: Struts 2 tutorial

Index.jsp:

60Prof. Vinod Pillai

Page 61: Struts 2 tutorial

Struts.xml:

61Prof. Vinod Pillai

Page 62: Struts 2 tutorial

Action Class :

62Prof. Vinod Pillai

Page 63: Struts 2 tutorial

Insert.jsp:

63Prof. Vinod Pillai

Page 64: Struts 2 tutorial

Update.jsp:

64Prof. Vinod Pillai

Page 65: Struts 2 tutorial

Output:

65Prof. Vinod Pillai

Page 66: Struts 2 tutorial

Output:

66Prof. Vinod Pillai

Page 67: Struts 2 tutorial

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

Page 68: Struts 2 tutorial

Final Structure:

68Prof. Vinod Pillai

Page 69: Struts 2 tutorial

Index.jsp:

69Prof. Vinod Pillai

Page 70: Struts 2 tutorial

Struts.xml:

70Prof. Vinod Pillai

Page 71: Struts 2 tutorial

Action Class:

71Prof. Vinod Pillai

Page 72: Struts 2 tutorial

Welcome.jsp:

72Prof. Vinod Pillai

Page 73: Struts 2 tutorial

Second Option : XML File Validation

73Prof. Vinod Pillai

Page 74: Struts 2 tutorial

Final Structure:

74Prof. Vinod Pillai

Page 75: Struts 2 tutorial

LoginAction-validation.xml :

75Prof. Vinod Pillai

Page 76: Struts 2 tutorial

Action Class :

76Prof. Vinod Pillai

Page 77: Struts 2 tutorial

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

Page 78: Struts 2 tutorial

Action Class :

78Prof. Vinod Pillai

Page 79: Struts 2 tutorial

Welcome.jsp:

79Prof. Vinod Pillai

Page 80: Struts 2 tutorial

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

Page 81: Struts 2 tutorial

Final Structure:

81Prof. Vinod Pillai

Page 82: Struts 2 tutorial

Index.jsp:

82Prof. Vinod Pillai

Page 83: Struts 2 tutorial

Struts.xml:

83Prof. Vinod Pillai

Page 84: Struts 2 tutorial

tiles.xml:

84Prof. Vinod Pillai

Page 85: Struts 2 tutorial

MainLayout.jsp:

85Prof. Vinod Pillai

Page 86: Struts 2 tutorial

Header.jsp:

Menu.jsp:

86Prof. Vinod Pillai

Page 87: Struts 2 tutorial

FirstPage.jsp:

SecondPage.jsp:

87Prof. Vinod Pillai

Page 88: Struts 2 tutorial

8 & 9“HttpServletRequest, HttpServletResponse, ServletContext

& HttpSession”Our Goal :

Getting ServletContext Object.

Getting HttpServletRequest Object.

Getting HttpServletResponse Object.

Getting HttpSession Object.

88Prof. Vinod Pillai

Page 89: Struts 2 tutorial

ApplicationAware/ServletContext:

89Prof. Vinod Pillai

Page 90: Struts 2 tutorial

SessionAware/HttpSession:

90Prof. Vinod Pillai

Page 91: Struts 2 tutorial

ServletRequestAware/HttpServletRequest:

91Prof. Vinod Pillai

Page 92: Struts 2 tutorial

ServletResponseAware :

92Prof. Vinod Pillai

Page 93: Struts 2 tutorial

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

Page 94: Struts 2 tutorial

Database Output:

94Prof. Vinod Pillai

Page 95: Struts 2 tutorial

Final Structure :

95Prof. Vinod Pillai

Page 96: Struts 2 tutorial

Index.jsp :

96Prof. Vinod Pillai

Page 97: Struts 2 tutorial

Struts.xml :

97Prof. Vinod Pillai

Page 98: Struts 2 tutorial

Login Action :

98Prof. Vinod Pillai

Page 99: Struts 2 tutorial

Login Action :

99Prof. Vinod Pillai

Page 100: Struts 2 tutorial

Login Action :

100Prof. Vinod Pillai

Page 101: Struts 2 tutorial

UserClass.java / POJO /Bean:

101Prof. Vinod Pillai

Page 102: Struts 2 tutorial

EditUser.jsp :

102Prof. Vinod Pillai

Page 103: Struts 2 tutorial

UpdateUserAction.java :

103Prof. Vinod Pillai

Page 104: Struts 2 tutorial

UpdateUserAction.java :

104Prof. Vinod Pillai

Page 105: Struts 2 tutorial

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.

Page 106: Struts 2 tutorial

Real Struts 2 Architecture

106Prof. Vinod Pillai

Page 107: Struts 2 tutorial

Thank You

[email protected]

107Prof. Vinod Pillai