1 / 57

Introduction to Struts 2.0

Introduction to Struts 2.0. Jenny Ni Joey Feng Winddays Wang Hewmmi Zhu Heather Lv. Contents. What is Struts? Why to use framework? Struts 2.0 Overview Struts 2.0 MVC components Request Lifecycle in Struts 2 Struts 2.0 Architecture Sample Application Why we should use Struts 2.0?

yaron
Télécharger la présentation

Introduction to Struts 2.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Struts 2.0 Jenny Ni Joey Feng Winddays Wang Hewmmi Zhu Heather Lv

  2. Contents • What is Struts? • Why to use framework? • Struts 2.0 Overview • Struts 2.0 MVC components • Request Lifecycle in Struts 2 • Struts 2.0 Architecture • Sample Application • Why we should use Struts 2.0? • Struts 1.x vs Struts 2.0 • What you need to start using Struts2.0

  3. What is Struts? • 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

  4. Why use framework? • 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.

  5. Struts 2.0 • Complete new framework based on webwork framework. • Struts 2.0 implements MVC 2 design pattern.

  6. Struts 2.0 MVC Components • 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.

  7. Struts 2.0 MVC Components contd. • 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

  8. Request Lifecycle in Struts 2.0 • User Sends Request • Filter Dispatcher determines the appropriate action • Interceptors are applied • Execution of action • Output Rendering • Return of Request • Display of result to user.

  9. Struts 2.0 Architecture

  10. Struts 2.0 Architecture

  11. Why we should use Struts 2.0? • Simplified Design • Simplified Action • Simplified Testability • Better tag features • Annotation introduced • Easy plug-in • AJAX Support

  12. Struts 1.x vs Struts 2.0 • 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

  13. Struts 1 Action  ActionForm  ActionForward  struts-config.xml  ActionServlet  RequestProcessor  Struts 2 Action Action or POJO’s Result struts.xml FilterDispatcher Interceptors

  14. What you need to start using Struts 2.0? • Java 5.0 • Tomcat 5.x(Servlet api 2.4 and jsp api 2.0)

  15. Let’s see something real…

  16. Web, Mail 2000

  17. Web, Mail 2009

  18. Struts Plugins

  19. Simple Example

  20. Struts 1 <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:

  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. </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. ... Only four pages! <td align="left"> <html:submit property="DO_SUBMIT"> Save </html:submit> </td> </tr> </table> </html:form>

  24. Struts 2 <s:form action="Meeting" validate="true"> <s:token /> <s:textfield label=”Name” name=“name” /> <s:textfieldlabel=”Date"name="date"/> <s:select label=”Invitees” name="invitees"list="employees"/> <s:textarealabel=”Description” name="description" rows="4"cols="50"/> <s:submitvalue=”Save"method="save"/> </s:form>

  25. Example Revisited

  26. <s:textfield label="Name" name="name"tooltip="Meeting name" />

  27. <s:datepicker label="Date" name="date"/>

  28. <s:optiontransferselect ... />

  29. <jsp:include page="/ajax/commonInclude.jsp"/> ... <s:textareatheme="ajax" label="Description" name="description" rows="4" cols="50" />

  30. But there's more . . .

  31. 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. Brian Kernighan Law of Debugging Difficulty

  32. Prevention and Cure

  33. struts.devMode = true

  34. Built-in Testing Support public class MyActionTest extends StrutsTestCase { public void testExecute() throws Exception { assertTrue(true); } }

  35. any.action?debug=console

More Related