Introduction to Struts 2 0 Jenny Ni Joey

























































- Slides: 57
Introduction to Struts 2. 0 Jenny Ni Joey Feng Winddays Wang Hewmmi Zhu Heather Lv 1 Software School , Fudan University
Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. What is Struts? Why to use framework? Struts 2. 0 Overview Struts 2. 0 MVC components Request Lifecycle in 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 Struts 2. 0 Software School , Fudan University 2
What is Struts? Open Source java framework for creating web applications. v Action Based Framework v Create web application using MVC 2 architecture v Apache Struts offer two major version v v. Struts 1. x v. Struts 2. 0 v Struts 2 = Web. Work + Struts Software School , Fudan University 3
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: v Automation of common tasks v Concentrate on higher level concerns. Software School , Fudan University 4
Struts 2. 0 v v Complete new framework based on webwork framework. Struts 2. 0 implements MVC 2 design pattern. Software School , Fudan University 5
Struts 2. 0 MVC Components v Controller: Filter Dispatcher: v First component that start processing that is why this type of MVC is called front controller MVC v Looks at the request and apply the appropriate action. v Struts framework handles all of the controller work. v Its configured in web. xml v Interceptors: v Can execute code before and after an Action is executed. v They can be configured per action basis. v Can be used for data validation, file upload, double submit guards. Software School , Fudan University 6
Struts 2. 0 MVC Components contd. Model: v Implemented by action class v For model you can use any data access technologies like JDBC, EJB, Hibernate View v Its your result part. It can be JSP, JSTL, JSF etc. v Presentation part of the MVC Software School , Fudan University 7
Request Lifecycle in Struts 2. 0 1. 2. 3. 4. 5. 6. 7. 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. Software School , Fudan University 8
Struts 2. 0 Architecture
Struts 2. 0 Architecture Software School , Fudan University 10
Why we should use Struts 2. 0? 1. 2. 3. 4. 5. 6. 7. Simplified Design Simplified Action Simplified Testability Better tag features Annotation introduced Easy plug-in AJAX Support Software School , Fudan University 11
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 Software School , Fudan University 12
Struts 1 Action. Form Action. Forward struts-config. xml Action. Servlet Request. Processor Struts 2 Action or POJO’s Result struts. xml Filter. Dispatcher Interceptors
What you need to start using Struts 2. 0? Java 5. 0 2. Tomcat 5. x(Servlet api 2. 4 and jsp api 2. 0) 1. Software School , Fudan University 14
Let’s see something real… Software School , Fudan University 15
Web, Mail 2000
Web, Mail 2009
Struts Plugins
Simple Example
Struts 1 <html: errors/> <html: form action="/Save. Meeting"> <table border="0" width="100%"> <tr> <th align="right"> Name: </th> <td align="left"> <html: text property="name" size=” 50” /> </td> </tr> <th align="right"> Date:
</th> <td align="left"> <html: text property="date" size="50"/> </td> </tr> <th align="right"> Invitees: </th> <td align="left"> <html: select property="invitees" multiple="true"> <html: options collection="employees" property="value" label. Property="label"/> </html: select>
</tr> <th align="right"> Description: </th> <td align="left"> <html: textarea property="description" rows="4" cols="50" /> </td> </tr> <td align="right"> </td>
<td align="left"> <html: submit property="DO_SUBMIT"> Save </html: submit> </td> </tr> </table> </html: form> . . . Only four pages!
Struts 2 <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>
Example Revisited
<s: textfield label="Name" name="name" tooltip="Meeting name" />
<s: datepicker label="Date" name="date"/>
<s: optiontransferselect. . . />
<jsp: include page="/ajax/common. Include. jsp"/>. . . <s: textarea theme="ajax" label="Description" name="description" rows="4" cols="50" />
But there's more. . .
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.
Prevention and Cure
struts. dev. Mode = true
Built-in Testing Support public class My. Action. Test extends Struts. Test. Case { public void test. Execute() throws Exception { assert. True(true); } }
any. action? debug=console
any. action? profiling=yes
Time to Upgrade?
Tutorials, Guides, and FAQs
Struts 2 Training Course
Run Struts 1 Actions as Is <action name="edit. Gangster" class="org. apache. struts 2. s 1. Struts 1 Action"> <param name="class. Name"> com. mycompany. gangstas. Edit. Gangster. Action </param> <result> gangster. Form. jsp </result> </action>
How do I get started?
Where We are Going

























































