Prof. Vinod Pillai.vinodthebest@gmail.comhttp://vinodthebest.wordpress.comwww.youtube.com/vinodthebest
Part – IISetting Up  Struts   Prof. Vinod Pillai   2
Part – II : Setting Up Struts & Runningbasic App.   Setting Up Struts 2 & Running Hello World Application.   Action Clas...
Setting Up Struts*Before we start we need the following tools: JDK 1.7 (Download) Eclipse Java EE IDE for Web Developers...
Apache Struts 2http://struts.apache.org/download.cgi Pillai                               Prof. Vinod     5
MySQLhttp://dev.mysql.com/downloads/ Vinod Pillai                             Prof.             6
Setting Up Struts JDK 1.7 = .exe Eclipse Java EE IDE = zip Tomcat 7 = zip Apache Struts-2.0.6 JAR = zip MySQL Databas...
Setting Up Struts           Prof. Vinod Pillai   8
1      “Welcome to Apache Struts 2”Our Goal : Our goal is to create a basic Struts2 application with a  Submit Button. U...
 Open Eclipse and go to File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.          ...
 Project Name: Welcome Struts. Target runtime: <Select New Runtime> & show the path where you have unzipped the Tomcat Se...
 Copy JAR files in WebContent -> WEB-INF -> lib folder.                       Prof. Vinod Pillai                  12
 The entry point of Struts2 application will be the Filter  define in deployment descriptor (web.xml). Hence we will  def...
web.xml          Prof. Vinod Pillai   14
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 o...
index.jsp<%@ taglib prefix="s" uri="/struts-tags" %>                      Prof. Vinod Pillai      16
Welcome.jspError.jsp              Prof. Vinod Pillai   17
The Action Class We will need an Action class that will accept the request and return success. For this we will create a ...
struts.xml Struts2 reads the configuration and class definition from an  xml file called struts.xml. This file is loaded ...
Run the Application: To run the project, right click on Project name from Project  Explorer and select Run as -> Run on S...
2“Action & Form – Name & Last name –Our Goal :              Display.” To create a Struts2 application with a index page. ...
Final Structure:                   Prof. Vinod Pillai   22
index.jsp            Prof. Vinod Pillai   23
struts.xml             Prof. Vinod Pillai   24
Action Class               Prof. Vinod Pillai   25
Welcome.jsp              Prof. Vinod Pillai   26
3“Action Class (POJO/Plain Old Java Object)                 & Form” Our Goal :  To create a Struts2 application with an i...
Final Structure:                   Prof. Vinod Pillai   28
Action Class:                Prof. Vinod Pillai   29
“Important Points:”Note: Here the action class doesnt extend any other class or  interface. The method invoked in the pro...
Note:        Prof. Vinod Pillai   31
Note:        Prof. Vinod Pillai   32
Interceptors Interceptors are one of the most powerful features  of Struts 2. As Interceptor, as the name suggests, inte...
Interceptors The interceptor approach helps in modularizing  common code into reusable classes. The framework provides a...
Interceptors Observe that in the figure the before Action class is  executed, request passes through the set of  Intercep...
Interceptors Creating Custom Interceptor : [Two Ways]   Implementing Interceptor Interface.   Extending AbstractInterce...
4                    “Interceptors”Our Goal : To create a Struts2 application with an index page. User  clicks the submit...
Final Structure:                   Prof. Vinod Pillai   38
Interceptor Class:                     Prof. Vinod Pillai   39
Struts.xml:              Prof. Vinod Pillai   40
Output:          Prof. Vinod Pillai   41
Note: With most web applications, we find ourselves wanting to  apply the same set of Interceptors over and over again.  ...
Interceptor Class: [Getting Form Value & making changes]                        Prof. Vinod Pillai                 43
Output :           Prof. Vinod Pillai   44
5“Common Action Class – Multiple ExecuteOur Goal :   Method ” To create a Struts2 application with an index page. When th...
Final Structure:                   Prof. Vinod Pillai   46
Index.jsp:             Prof. Vinod Pillai   47
Struts.xml:              Prof. Vinod Pillai   48
Action Class :                 Prof. Vinod Pillai   49
Insert.jsp:              Prof. Vinod Pillai   50
Update.jsp:              Prof. Vinod Pillai   51
Output:          Prof. Vinod Pillai   52
Output:          Prof. Vinod Pillai   53
Part – II : Setting Up Struts & Runningbasic App.   Setting Up Struts 2 & Running Hello World Application.   Action Clas...
Part – IICompleted   Prof. Vinod Pillai   55
Thank Youvinodthebest@gmail.com              Prof. Vinod Pillai   56
Upcoming SlideShare
Loading in...5
×

Vinod sir struts 2 part 2

7,092
-1

Published on

Configuring Struts 2 in Eclipse, Action Class and Interceptors.

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

No Downloads
Views
Total Views
7,092
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
121
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Vinod sir struts 2 part 2

  1. 1. Prof. Vinod Pillai.vinodthebest@gmail.comhttp://vinodthebest.wordpress.comwww.youtube.com/vinodthebest
  2. 2. Part – IISetting Up Struts Prof. Vinod Pillai 2
  3. 3. Part – II : Setting Up Struts & Runningbasic 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
  4. 4. 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 4
  5. 5. Apache Struts 2http://struts.apache.org/download.cgi Pillai Prof. Vinod 5
  6. 6. MySQLhttp://dev.mysql.com/downloads/ Vinod Pillai Prof. 6
  7. 7. 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 7
  8. 8. Setting Up Struts Prof. Vinod Pillai 8
  9. 9. 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 9
  10. 10.  Open Eclipse and go to File -> New -> Project and select Dynamic Web Project in the New Project wizard screen. Prof. Vinod Pillai 10
  11. 11.  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 11
  12. 12.  Copy JAR files in WebContent -> WEB-INF -> lib folder. Prof. Vinod Pillai 12
  13. 13.  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 13
  14. 14. web.xml Prof. Vinod Pillai 14
  15. 15. 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 15
  16. 16. index.jsp<%@ taglib prefix="s" uri="/struts-tags" %> Prof. Vinod Pillai 16
  17. 17. Welcome.jspError.jsp Prof. Vinod Pillai 17
  18. 18. 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 18
  19. 19. 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 19
  20. 20. 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 20
  21. 21. 2“Action & Form – Name & Last name –Our Goal : Display.” 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 21
  22. 22. Final Structure: Prof. Vinod Pillai 22
  23. 23. index.jsp Prof. Vinod Pillai 23
  24. 24. struts.xml Prof. Vinod Pillai 24
  25. 25. Action Class Prof. Vinod Pillai 25
  26. 26. Welcome.jsp Prof. Vinod Pillai 26
  27. 27. 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 27
  28. 28. Final Structure: Prof. Vinod Pillai 28
  29. 29. Action Class: Prof. Vinod Pillai 29
  30. 30. “Important Points:”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. Try : [ActionSupport , Action & POJO ]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 30
  31. 31. Note: Prof. Vinod Pillai 31
  32. 32. Note: Prof. Vinod Pillai 32
  33. 33. 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. 33
  34. 34. 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 34
  35. 35. Interceptors Observe that in the figure the before Action class is executed, request passes through the set of Interceptors which provide request pre-processing. 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 35
  36. 36. Interceptors Creating Custom Interceptor : [Two Ways]  Implementing Interceptor Interface.  Extending AbstractInterceptor Class. Configuring Interceptors: Prof. Vinod Pillai 36
  37. 37. 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 37
  38. 38. Final Structure: Prof. Vinod Pillai 38
  39. 39. Interceptor Class: Prof. Vinod Pillai 39
  40. 40. Struts.xml: Prof. Vinod Pillai 40
  41. 41. Output: Prof. Vinod Pillai 41
  42. 42. 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 42
  43. 43. Interceptor Class: [Getting Form Value & making changes] Prof. Vinod Pillai 43
  44. 44. Output : Prof. Vinod Pillai 44
  45. 45. 5“Common Action Class – Multiple ExecuteOur Goal : Method ” 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 45
  46. 46. Final Structure: Prof. Vinod Pillai 46
  47. 47. Index.jsp: Prof. Vinod Pillai 47
  48. 48. Struts.xml: Prof. Vinod Pillai 48
  49. 49. Action Class : Prof. Vinod Pillai 49
  50. 50. Insert.jsp: Prof. Vinod Pillai 50
  51. 51. Update.jsp: Prof. Vinod Pillai 51
  52. 52. Output: Prof. Vinod Pillai 52
  53. 53. Output: Prof. Vinod Pillai 53
  54. 54. Part – II : Setting Up Struts & Runningbasic 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
  55. 55. Part – IICompleted Prof. Vinod Pillai 55
  56. 56. Thank Youvinodthebest@gmail.com Prof. Vinod Pillai 56
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×