Introduction to Struts 2.0

57
Introduction to Struts 2.0 Jenny Ni Joey Feng Winddays Wang Hewmmi Zhu Heather Lv Software School ,Fudan University 1

description

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? - PowerPoint PPT Presentation

Transcript of Introduction to Struts 2.0

Page 1: Introduction to Struts 2.0

Introduction to Struts 2.0

Jenny NiJoey Feng

Winddays WangHewmmi Zhu

Heather Lv

Software School ,Fudan University1

Page 2: Introduction to Struts 2.0

1. What is Struts?2. Why to use framework?3. Struts 2.0 Overview4. Struts 2.0 MVC components5. Request Lifecycle in Struts 26. Struts 2.0 Architecture7. Sample Application8. 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

Software School ,Fudan University 2

Page 3: Introduction to Struts 2.0

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.xStruts 2.0

Struts 2 = WebWork + Struts

What is Struts?

Software School ,Fudan University 3

Page 4: Introduction to Struts 2.0

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 tasksConcentrate on higher level concerns.

Why use framework?

Software School ,Fudan University 4

Page 5: Introduction to Struts 2.0

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

Struts 2.0

Software School ,Fudan University 5

Page 6: Introduction to Struts 2.0

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

Software School ,Fudan University 6

Page 7: Introduction to Struts 2.0

Model:-Implemented by action classFor model you can use any data access

technologies like JDBC,EJB,Hibernate

ViewIts your result part. It can be JSP,JSTL,JSF etc.Presentation part of the MVC

Struts 2.0 MVC Components contd.

Software School ,Fudan University 7

Page 8: Introduction to Struts 2.0

1. User Sends Request2. Filter Dispatcher determines the appropriate action3. Interceptors are applied4. Execution of action5. Output Rendering6. Return of Request7. Display of result to user.

Request Lifecycle in Struts 2.0

Software School ,Fudan University 8

Page 9: Introduction to Struts 2.0

Struts 2.0 Architecture

Page 10: Introduction to Struts 2.0

Struts 2.0 Architecture

Software School ,Fudan University 10

Page 11: Introduction to Struts 2.0

1. Simplified Design2. Simplified Action3. Simplified Testability4. Better tag features5. Annotation introduced6. Easy plug-in7. AJAX Support

Why we should use Struts 2.0?

Software School ,Fudan University 11

Page 12: Introduction to 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

Struts 1.x vs Struts 2.0

Software School ,Fudan University 12

Page 13: Introduction to Struts 2.0

Struts 1Action ActionForm ActionForward struts-config.xml

ActionServlet RequestProcessor

Struts 2ActionAction or POJO’sResultstruts.xmlFilterDispatcherInterceptors

Page 14: Introduction to Struts 2.0

1. Java 5.02. Tomcat 5.x(Servlet api 2.4 and jsp api

2.0)

What you need to start using Struts 2.0?

Software School ,Fudan University 14

Page 15: Introduction to Struts 2.0

Let’s see something real…

Software School ,Fudan University 15

Page 16: Introduction to Struts 2.0

Web, Mail 2000

Page 17: Introduction to Struts 2.0

Web, Mail 2009

Page 18: Introduction to Struts 2.0
Page 19: Introduction to Struts 2.0
Page 20: Introduction to Struts 2.0
Page 21: Introduction to Struts 2.0
Page 22: Introduction to Struts 2.0
Page 23: Introduction to Struts 2.0
Page 24: Introduction to Struts 2.0
Page 25: Introduction to Struts 2.0
Page 26: Introduction to Struts 2.0
Page 27: Introduction to Struts 2.0
Page 28: Introduction to Struts 2.0
Page 29: Introduction to Struts 2.0
Page 30: Introduction to Struts 2.0
Page 31: Introduction to Struts 2.0

Struts Plugins

Page 32: Introduction to Struts 2.0

Simple Example

Page 33: Introduction to Struts 2.0

<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

Page 34: Introduction to Struts 2.0

</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>

Page 35: Introduction to Struts 2.0

</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>

Page 36: Introduction to Struts 2.0

...

Only four pages!

<td align="left"> <html:submit property="DO_SUBMIT"> Save </html:submit> </td></tr></table></html:form>

Page 37: Introduction to Struts 2.0

<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

Page 38: Introduction to Struts 2.0

Example Revisited

Page 39: Introduction to Struts 2.0

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

Page 40: Introduction to Struts 2.0

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

Page 41: Introduction to Struts 2.0

<s:optiontransferselect ... />

Page 42: Introduction to Struts 2.0

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

Page 43: Introduction to Struts 2.0

But there's more . . .

Page 44: Introduction to Struts 2.0

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.

Page 45: Introduction to Struts 2.0

Prevention and Cure

Page 46: Introduction to Struts 2.0

struts.devMode = true

Page 47: Introduction to Struts 2.0
Page 48: Introduction to Struts 2.0
Page 49: Introduction to Struts 2.0

Built-in Testing Support

public class MyActionTest extends StrutsTestCase {

public void testExecute() throws Exception {

assertTrue(true); }}

Page 50: Introduction to Struts 2.0

any.action?debug=console

Page 51: Introduction to Struts 2.0

any.action?profiling=yes

Page 52: Introduction to Struts 2.0

Time to Upgrade?

Page 53: Introduction to Struts 2.0

Tutorials, Guides, and FAQs

Page 54: Introduction to Struts 2.0

Struts 2 Training Course

Page 55: Introduction to Struts 2.0

<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

Page 56: Introduction to Struts 2.0

How do I get started?

Page 57: Introduction to Struts 2.0

Where We are Going