Your SlideShare is downloading. ×
0
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Struts 2 tutorial
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Struts 2 tutorial

6,326

Published on

Struts 2 Tutorial with Examples.

Struts 2 Tutorial with Examples.

Published in: Technology, Education
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,326
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
633
Comments
0
Likes
9
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Presented By: Prof. Vinod Pillai.(M) 9909023599vinodvpillai@hotmail.comhttp://vinodthebest.wordpress.com
  • 2. 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. Prof. Vinod Pillai 2
  • 3. Part – IUnderstanding Struts Prof. Vinod Pillai 3
  • 4. Introduction Java Application = Desktop || Web || Mobile. Struts = Apache Struts Web Framework. Framework:  Peace of Software that Automates all tedious task.  Use Design patterns commonly agreed by Industry.  In built features that commonly needed by most of project. Prof. Vinod Pillai 4
  • 5. 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, with version 1.0 officially released in July 2001. Why we need it?  Web applications based on JSP sometimes combines database code, page design code, and control flow code.  In practice, we find that unless these concerns are separated, larger applications become difficult to maintain. Prof. Vinod Pillai 5
  • 6. 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 developers create web applications that utilize a MVC architecture. Prof. Vinod Pillai 6
  • 7. Model View Controller (MVC) Prof. Vinod Pillai 7
  • 8. Struts 2 Apache Struts Project offers two major versions:  Struts 1 is recognized as the most popular web application framework for Java.  Struts 2 was originally known as WebWork 2.WebWork and Struts communities joined forces to create Struts 2. Struts 2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action. Action class in Struts 2 act as the model in the web application. Unlike Struts, Struts 2 Action class are plain POJO objects thus simplifying the testing of the code. Prof. Vinod Pillai 8
  • 9. Struts 2 Struts2 also comes with power APIs to configure Interceptors that reduce greatly the coupling in application. The view part of Struts 2 is highly configurable and it supports different result-types such as Velocity, FreeMarker, JSP, etc. Prof. Vinod Pillai 9
  • 10. Struts 2 Architecture Prof. Vinod Pillai 10
  • 11. Struts 2 Architecture1. Request is generated by user and sent to Servlet container.2. Servlet container invokes FilterDispatcher filter which in turn determines appropriate action.3. One by one Intercetors are applied before calling the Action. Interceptors performs tasks such as Logging, Validation, File Upload, Double-submit guard etc.4. Action is executed and the Result is generated by Action.5. The output of Action is rendered in the view (JSP, Velocity, etc) and the result is returned to the user. Prof. Vinod Pillai 11
  • 12. Part – IISetting Up Struts Prof. Vinod Pillai 12
  • 13. 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. Prof. Vinod Pillai 13
  • 14. JDK 1.7http://www.oracle.com/technetwork/java/javase/downloads/index.html Prof. Vinod Pillai 14
  • 15. Eclipse Java EE IDEhttp://eclipse.org/downloads/ Prof. Vinod Pillai 15
  • 16. Apache Tomcat 7http://tomcat.apache.org/download-70.cgi Prof. Vinod Pillai 16
  • 17. Apache Struts 2http://struts.apache.org/download.cgi Pillai Prof. Vinod 17
  • 18. MySQLhttp://dev.mysql.com/downloads/ Vinod Pillai Prof. 18
  • 19. Setting Up Struts JDK 1.7 = .exe Eclipse Java EE IDE = zip Tomcat 7 = zip Apache Struts-2.0.6 JAR = zip MySQL Database = .exeSimply Install the software & make sure you remember the path. Prof. Vinod Pillai 19
  • 20. Setting Up Struts Prof. Vinod Pillai 20
  • 21. 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”. Prof. Vinod Pillai 21
  • 22. Getting Started Start Eclipse and enter the workspace name: Prof. Vinod Pillai 22
  • 23.  Open Eclipse and go to File -> New -> Project and select Dynamic Web Project in the New Project wizard screen. Prof. Vinod Pillai 23
  • 24.  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. Prof. Vinod Pillai 24
  • 25.  Copy JAR files in WebContent -> WEB-INF -> lib folder. Prof. Vinod Pillai 25
  • 26.  The entry point of Struts2 application will be the Filter define in deployment descriptor (web.xml). Hence we will define an entry of org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExe cuteFilter class in web.xml. Open web.xml file which is under WEB-INF folder and copy paste following code. Prof. Vinod Pillai 26
  • 27. web.xml Prof. Vinod Pillai 27
  • 28. 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 Prof. Vinod Pillai 28
  • 29. index.jsp Prof. Vinod Pillai 29
  • 30. Welcome.jspError.jsp Prof. Vinod Pillai 30
  • 31. 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. Prof. Vinod Pillai 31
  • 32. The Action Class We will need an Action class that will accept the request and return success. For this we will create a package com.vinod.struts in the source folder/src. This package will contain the action file. Prof. Vinod Pillai 32
  • 33. 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. Prof. Vinod Pillai 33
  • 34. Part – IIIApplicationDevelopment Prof. Vinod Pillai 34
  • 35. 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>. Prof. Vinod Pillai 35
  • 36. Final Structure: Prof. Vinod Pillai 36
  • 37. index.jsp Prof. Vinod Pillai 37
  • 38. struts.xml Prof. Vinod Pillai 38
  • 39. Action Class Prof. Vinod Pillai 39
  • 40. Welcome.jsp Prof. Vinod Pillai 40
  • 41. 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. Prof. Vinod Pillai 41
  • 42. 3“Action Class (POJO/Plain Old Java Object) & Form” Our Goal :  To create a Struts2 application with an index page. User will enter his First Name, Last Name and Address and it will be redirected to a Welcome page which will display <Name> <Last Name> & <Address>.  Same as the above application only change is here that Action Class is a POJO Class.  Other all same only change in Action Class. Prof. Vinod Pillai 42
  • 43. Final Structure: Prof. Vinod Pillai 43
  • 44. Action Class: Prof. Vinod Pillai 44
  • 45. Note: Here the action class doesnt extend any other class or interface. The method invoked in the processing of an action is the "execute" method.How this happened? All actions may implement this interface, which exposes the execute() method. You are free to create POJOs that maintains the same contract defined by this interface without actually implementing the interface. Prof. Vinod Pillai 45
  • 46. Note: Prof. Vinod Pillai 46
  • 47. Note: Prof. Vinod Pillai 47
  • 48. Interceptors Interceptors are one of the most powerful features of Struts 2. As Interceptor, as the name suggests, intercepts the request, and provides some additional processing before and after the execution of action and result. Common functionalities required by each action are implemented as Interceptors. These functionalities may include validation of input data, pre-processing of file upload, and sometimes pre-processing of controls with some data before the web pagePillai Prof. Vinod appears, etc. 48
  • 49. Interceptors The interceptor approach helps in modularizing common code into reusable classes. The framework provides a set of Interceptors, which can be used to provide the required functionalities to an action. User can create his custom interceptors, to get the functionality required for each action. Prof. Vinod Pillai 49
  • 50. Interceptors Observe that in the figure the Action class is executed, then a request passes through the set of Interceptors which provide request pre-processing which is required before the action is executed. Similarly after the execution of Action class all Interceptors are executed once more to provide post-processing if any. But this time the execution order of Interceptors is reversed. Prof. Vinod Pillai 50
  • 51. Interceptors Creating Custom Interceptor : [Two Ways]  Implementing Interceptor Interface.  Extending AbstractInterceptor Class. Configuring Interceptors: Prof. Vinod Pillai 51
  • 52. 4 “Interceptors”Our Goal : To create a Struts2 application with an index page. User clicks the submit button before passing the request to Action it will be send to Interceptor then it will go to Action and finally before showing the result it will send to Interceptor and then to Welcome.jsp/result. Prof. Vinod Pillai 52
  • 53. Final Structure: Prof. Vinod Pillai 53
  • 54. Interceptor Class: Prof. Vinod Pillai 54
  • 55. Struts.xml: Prof. Vinod Pillai 55
  • 56. Output: Prof. Vinod Pillai 56
  • 57. Note: With most web applications, we find ourselves wanting to apply the same set of Interceptors over and over again. Rather than reiterate the same list of Interceptors, we can bundle these Interceptors together using an Interceptor Stack. Prof. Vinod Pillai 57
  • 58. 5“Common Action Class – Multiple Execute Method ”Our Goal : To create a Struts2 application with an index page. When the user clicks the Update or Insert Button both goes to same Action Class but different execute for each button. Prof. Vinod Pillai 58
  • 59. Final Structure: Prof. Vinod Pillai 59
  • 60. Index.jsp: Prof. Vinod Pillai 60
  • 61. Struts.xml: Prof. Vinod Pillai 61
  • 62. Action Class : Prof. Vinod Pillai 62
  • 63. Insert.jsp: Prof. Vinod Pillai 63
  • 64. Update.jsp: Prof. Vinod Pillai 64
  • 65. Output: Prof. Vinod Pillai 65
  • 66. Output: Prof. Vinod Pillai 66
  • 67. 6“Validation – Using validate () Method & XML”Our Goal : To create a Struts2 application with an index page. If user clicks the Login Button without entering the Username or Password then it should display message appropriately error message. Prof. Vinod Pillai 67
  • 68. Final Structure: Prof. Vinod Pillai 68
  • 69. Index.jsp: Prof. Vinod Pillai 69
  • 70. Struts.xml: Prof. Vinod Pillai 70
  • 71. Action Class: Prof. Vinod Pillai 71
  • 72. Welcome.jsp: Prof. Vinod Pillai 72
  • 73. Second Option : XML File Validation Prof. Vinod Pillai 73
  • 74. Final Structure: Prof. Vinod Pillai 74
  • 75. LoginAction-validation.xml : Prof. Vinod Pillai 75
  • 76. Action Class : Prof. Vinod Pillai 76
  • 77. 7Our Goal : “User Form” To create a Struts2 application with an index page. If user clicks the Login Button it will redirect him to Welcome page where some field values are coming from Action Class variable & some major Form Fields. Prof. Vinod Pillai 77
  • 78. Action Class : Prof. Vinod Pillai 78
  • 79. Welcome.jsp: Prof. Vinod Pillai 79
  • 80. 8Our Goal : “Integrating Tiles Plug-in” To create a Struts2 application with an index page. As user clicks the Login Button it will redirect him to Welcome page which having a common design layout which will be common for all the pages. Prof. Vinod Pillai 80
  • 81. Final Structure: Prof. Vinod Pillai 81
  • 82. Index.jsp: Prof. Vinod Pillai 82
  • 83. Struts.xml: Prof. Vinod Pillai 83
  • 84. tiles.xml: Prof. Vinod Pillai 84
  • 85. MainLayout.jsp: Prof. Vinod Pillai 85
  • 86. Header.jsp:Menu.jsp: Prof. Vinod Pillai 86
  • 87. FirstPage.jsp:SecondPage.jsp: Prof. Vinod Pillai 87
  • 88. 8&9“HttpServletRequest, HttpServletResponse, ServletContext & HttpSession” Our Goal :  Getting ServletContext Object.  Getting HttpServletRequest Object.  Getting HttpServletResponse Object.  Getting HttpSession Object. Prof. Vinod Pillai 88
  • 89. ApplicationAware/ServletContext: Prof. Vinod Pillai 89
  • 90. SessionAware/HttpSession: Prof. Vinod Pillai 90
  • 91. ServletRequestAware/HttpServletRequest: Prof. Vinod Pillai 91
  • 92. ServletResponseAware : Prof. Vinod Pillai 92
  • 93. 10 “Database – Insert + Validation + View + Update”Our Goal : To create a Struts2 Final application in we will provide user two options:  First : Allow to create a New Account and the data will entered to Database.  Second : Allow user to login into the system and view his details + Update his details. Major features:  Database connectivity.  HttpServletRequest.  Session. Prof. Vinod Pillai 93
  • 94. Database Output: Prof. Vinod Pillai 94
  • 95. Final Structure : Prof. Vinod Pillai 95
  • 96. Index.jsp : Prof. Vinod Pillai 96
  • 97. Struts.xml : Prof. Vinod Pillai 97
  • 98. Login Action : Prof. Vinod Pillai 98
  • 99. Login Action : Prof. Vinod Pillai 99
  • 100. Login Action : Prof. Vinod Pillai 100
  • 101. UserClass.java / POJO /Bean: Prof. Vinod Pillai 101
  • 102. EditUser.jsp : Prof. Vinod Pillai 102
  • 103. UpdateUserAction.java : Prof. Vinod Pillai 103
  • 104. UpdateUserAction.java : Prof. Vinod Pillai 104
  • 105. Conclusion: 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. Prof. Vinod Pillai 105
  • 106. Real Struts 2 Architecture Prof. Vinod Pillai 106
  • 107. Thank Youvinodvpillai@hotmail.com Prof. Vinod Pillai 107

×