Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Struts2

325 views

Published on

Struts2

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Struts2

  1. 1. Introduction to Struts 2.0 1
  2. 2. 1. What is Struts? 2. Why to use framework? 3. Struts 2.0 Overview 4. Struts 2.0 MVC components 5. Request Lifecycle in Struts 2 6. Struts 2.0 Architecture 7. Sample Application 8. Why we should use Struts 2.0? 9. Struts 1.x vs Struts 2.0 10. What you need to start using Struts2.0 Contents 2
  3. 3.  Open Source java framework for creating web applications.  Action Based Framework  Create web application using MVC 2 architecture  Apache Struts offer two major version Struts 1.x Struts 2.0  Struts 2 = WebWork + Struts What is Struts? 3
  4. 4.  Do we need framework?  No and Yes.  No.  In small applications where you don’t want the overhead of learning new things.  But  Yes  We have to use framework in real world application because:- Automation of common tasks Concentrate on higher level concerns. Why use framework? 4
  5. 5.  Complete new framework based on webwork framework.  Struts 2.0 implements MVC 2 design pattern. Struts 2.0 5
  6. 6.  Controller:-  Filter Dispatcher:-  First component that start processing that is why this type of MVC is called front controller MVC  Looks at the request and apply the appropriate action.  Struts framework handles all of the controller work.  Its configured in web.xml  Interceptors:-  Can execute code before and after an Action is executed.  They can be configured per action basis.  Can be used for data validation, file upload, double submit guards. Struts 2.0 MVC Components 6
  7. 7.  Model:- Implemented by action class For model you can use any data access technologies like JDBC,EJB,Hibernate  View Its your result part. It can be JSP,JSTL,JSF etc. Presentation part of the MVC Struts 2.0 MVC Components contd. 7
  8. 8. 1. User Sends Request 2. Filter Dispatcher determines the appropriate action 3. Interceptors are applied 4. Execution of action 5. Output Rendering 6. Return of Request 7. Display of result to user. Request Lifecycle in Struts 2.0 8
  9. 9. Struts 2.0 Architecture
  10. 10. Struts 2.0 Architecture 10
  11. 11. 1. Simplified Design 2. Simplified Action 3. Simplified Testability 4. Better tag features 5. Annotation introduced 6. Easy plug-in 7. AJAX Support Why we should use Struts 2.0? 11
  12. 12.  How Struts 1.x and Struts 2.0 differ from each other? › Configuration › Action Class › Dependency injection. › Servlet Dependency › Validation › Threading model › Testability › Expression Language Struts 1.x vs Struts 2.0 12
  13. 13. Struts 1 Action ⇒ ActionForm ⇒ ActionForward ⇒ struts-config.xml ⇒ ActionServlet ⇒ RequestProcessor ⇒ Struts 2 Action Action or POJO’s Result struts.xml FilterDispatcher Interceptors
  14. 14. 1. Java 5.0 2. Tomcat 5.x(Servlet api 2.4 and jsp api 2.0) What you need to start using Struts 2.0? 14
  15. 15. Let’s see something real… 15
  16. 16. Web, Mail 2000
  17. 17. Web, Mail 2009
  18. 18. Struts Plugins
  19. 19. Simple Example
  20. 20. <html:errors/> <html:form action="/SaveMeeting"> <table border="0" width="100%"> <tr> <th align="right"> Name: </th> <td align="left"> <html:text property="name" size=”50” /> </td> </tr> <tr> <th align="right"> Date: Struts 1
  21. 21. </th> <td align="left"> <html:text property="date" size="50"/> </td> </tr> <tr> <th align="right"> Invitees: </th> <td align="left"> <html:select property="invitees" multiple="true"> <html:options collection="employees" property="value" labelProperty="label"/> </html:select>
  22. 22. </tr> <tr> <th align="right"> Description: </th> <td align="left"> <html:textarea property="description" rows="4" cols="50" /> </td> </tr> <tr> <td align="right"> &nbsp; </td>
  23. 23.  ...  Only four pages! <td align="left"> <html:submit property="DO_SUBMIT"> Save </html:submit> </td> </tr> </table> </html:form>
  24. 24. <s:form action="Meeting" validate="true">   <s:token />   <s:textfield label=”Name”    name=“name” />   <s:textfield label=”Date"      name="date"/>   <s:select    label=”Invitees” name="invitees"  list="employees"/>   <s:textarea label=”Description”  name="description"                       rows="4" cols="50" />   <s:submit value=”Save" method="save"/> </s:form> Struts 2
  25. 25. Example Revisited
  26. 26. <s:textfield label="Name" name="name" tooltip="Meeting name" />
  27. 27. <s:datepicker label="Date" name="date"/>
  28. 28. <s:optiontransferselect ... />
  29. 29. <jsp:include page="/ajax/commonInclude.jsp"/> ... <s:textarea theme="ajax" label="Description" name="description" rows="4" cols="50" />
  30. 30. But there's more . . .
  31. 31. Brian Kernighan Law of Debugging Difficulty Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
  32. 32. struts.devMode = true
  33. 33. Built-in Testing Support public class MyActionTest extends StrutsTestCase { public void testExecute() throws Exception { assertTrue(true); } }
  34. 34. any.action?debug=console
  35. 35. any.action?profiling=yes
  36. 36. Tutorials, Guides, and FAQs
  37. 37. Struts 2 Training Course
  38. 38. <action name="editGangster" class="org.apache.struts2.s1.Struts1Action"> <param name="className"> com.mycompany.gangstas.EditGangsterAction </param> <result> gangsterForm.jsp </result> </action> Run Struts 1 Actions as Is

×