Sie sind auf Seite 1von 37

Surachai Krewatchai Sun Certified Java Programmer BrainStream Co.,Ltd. www.bsthome.com Surachai@bsthome.

com

www.bsthome.com

Struts

What is Struts?

Open-Source Jakarta project Started in 2000 by Craig McClanahan Currently at Version 1.1 Implementation

of the Model 2 MVC Architecture

www.bsthome.com

Struts

Why consider Struts?


Developed by Industry Experts Stable & Mature Manageable learning Curve Open Source

1700 member User Community ( 30,000 downloads per month


Its probably similar to what you would
Struts 3

50-100 new members each month)


build if not using Struts


www.bsthome.com

Why consider Struts?

Feature-rich

Free to develop & deploy


Many supported third-party tools Flexible and Extendable J2EE Technologies Expert Developers and Committers Large User Community Performance
Struts 4

www.bsthome.com

Struts Framework Features

Model 2 - MVC Implementation

Internationalization(I18N) Support
Rich JSP Tag Libraries Based on JSP, Servlet, XML, and Java

Supports Java Write Once, Run Anywhere Philosophy


Supports different model implementations (JavaBeans, Supports different presentation implementations ( JSP,

EJB, etc.)

XML/XSLT, JavaServer)
www.bsthome.com

Struts

Struts Dependencies

Java 1.2 or newer Servlet 2.2 and JSP 1.1 container XML parser compliant with JAXP 1.1 or Jakarta Commons packages

newer ( e.g. Xerces)

www.bsthome.com

Struts

The MVC Pattern


MVC Pattern history

In the beginning JSPs MVC Model 1

MVC Model 2 Pattern : The motivation for

the Struts Framework

www.bsthome.com

Struts

In The Beginning JSP


JSP tags

No more Java code in the HTML


Helps to separate page designer and Java

programmer roles.
Use the include mechanism to separate HTML components and Javascript

www.bsthome.com

Struts

MVC Model 1
View event Controller Create/Set Get model state Change Notification Model

www.bsthome.com

Good model for Java GUIs Web challenges: webs stateless behaviour view uses different technology than model or controllerStruts

MVC Model 2
request Controller (Servlet) Create/Set Model (JavaBean)

response View (JSP)

Get

Designed for the Web Architecture

www.bsthome.com

Struts

10

Struts MVC 2 Implementation

www.bsthome.com

Struts

11

Struts Main Class

www.bsthome.com

Struts

12

Struts Sequence

www.bsthome.com

Struts

13

Controller Components
ActionServlet

Struts supplied All client requests go through here Automatically populates a JavaBean Handles Locale and multi-part form requests

(ActionForm) with request parameters


Determines which Action or JSP to dispatch to

www.bsthome.com

Struts

14

Controller Components
Action

User defined Must extend org.apache.struts.action.Action Override the execute() or perform() method Acts as a coordinator to the Business Layer Gets data and determines which view to render

next

www.bsthome.com

Struts

15

Model Component
ActionForm

User defined Any JavaBean : Must have a public empty constructor

and getters/setters for the data elements

Can be implemented as any data element (JDO, EJB,

CORBA, etc.) : However, one should always decouple the client application from the specific business layer

implementation

Use Data Transfer Objects (Value Objects)


16

www.bsthome.com

Struts

View Component
JSP

User defined Uses the following:


Java Server Pages, HTML JavaScript, StyleSheets Struts Tags and Custom Tags

Resource Bundles
JavaBeans (model) ActionForms
Struts 17

www.bsthome.com

ActionForm Example

www.bsthome.com

Struts

18

Action Example

www.bsthome.com

Struts

19

JSP input Example

www.bsthome.com

Struts

20

JSP output Example

www.bsthome.com

Struts

21

struts-config.xml file

Contains the configuration data to hook all the Introduces a level of indirection to isolate the Four main types of information:

pieces together.

back end code (Actions and JSPs)

Message resource bundle declaration

Global forwards
Action Mappings Bean Declarations
Struts 22

www.bsthome.com

struts-config.xml file

www.bsthome.com

Struts

23

struts-config.xml file
Message resource bundle declaration

For internationalization Describes where to find the chai.properties file

in resources package(folder)

<message-resources parameter="resources.chai"/>

www.bsthome.com

Struts

24

struts-config.xml file
Global forwards

These are links to other pages that are a level of indirection, so that a path

referenced in other jsps or Actions.


Provides

change only has to updated one place.


<global-forwards>

<forward name="testForward" path="/TestView.jsp" />


</global-forwards>
www.bsthome.com

Struts

25

struts-config.xml file
Action Mappings

Defines a mapping between a logical Action name an associated ActionForm (name) the input page (input) to return to if errors

and the physical Action class (path, type)


Specifies Declares

occur
Defines

local forwards (forward tag). Can also use

forwards defined in the global-forwards section.


www.bsthome.com

Struts

26

struts-config.xml file
Action Mappings
<action-mappings> <action name="testActionForm" type="test.TestAction" validate="true" input="/TestJsp1.jsp" scope="request" path="/testAction" /> </action-mappings>
www.bsthome.com

Struts

27

struts-config.xml file
Bean Declarations

Used by the ActionServlet Controller when it the Controller where to physically locate the

automatically populates an ActionForm


Tells

referenced ActionForm
<form-beans>
<form-bean name="testActionForm" type="test.TestActionForm" />

</form-beans>
www.bsthome.com

Struts

28

Struts Tag Libraries used in View

HTML creates input forms

Bean accesses javabeans and resources


Logic manages conditionals Nested lets base tags relate to each other Template original page templating system Tiles new advanced templating system

www.bsthome.com

Struts

29

Struts Tag Libraries : Html Tags


Used to create input forms Tags are available for all of the HTML form input types.

www.bsthome.com

Struts

30

Struts Tag Libraries : Bean Tags


<bean:message>

Outputs a message stored in a resource bundle Provides the mechanism for internationalization

<bean:write>

Outputs a value from the given bean


Provides formatting attribute

<bean:message key="global.title"/>
<bean:write name="tableData" property="numberOfRows" />
www.bsthome.com

Struts

31

Struts Tag Libraries : Logic Tags

Conditional statements to determine output text

Examples: (empty, greaterThan, lessThan,present, iterate) Iterate allows looping over Collections
<logic:iterate name="tableData" property="dataList id="arow"> <tr><td> <bean:write name="arow" property="columnOne"/> </td><td> <bean:write name="arow" property="columnTwo"/> </td></tr> </ logic:iterate >
www.bsthome.com

Struts

32

Struts Tag Libraries : Nested Tags


An extension of the base tags Allows tags to be aware of the tags around them This relationship is based on the association of the Lets fewer attributes be defined

beans themselves

www.bsthome.com

Struts

33

Struts Tag Libraries : Nested Tags

<html:form action="/SubmitAction"> Person...<br> Name: <nested:text property="name"/><br> Age: <nested:text property="age" /><br> <nested:nest property="home"> Address...<br> City: <nested:text property="city" /><br> Province: <nested:text property="province" /> </nested:nest> </html:form>
www.bsthome.com

Struts

34

Other Features in Struts

File uploading

Multi-part form handling not defined in J2EE spec Struts provides implementation for doing this ActionForms Can define ActionForms in struts-config.xml No need to create an actual bean for each form Allows for multiple struts-config.xml files Large development teams can work on same project

Dynamic

Sub-Applications

DataSource definition

www.bsthome.com

Able to define datasources in the struts-config.xml


Struts

35

When Not To Use Struts

Struts gives a project increased flexibility, reBut, comes at the cost of added complexity

usability, and division of roles.

For medium to large projects it helps to manage


smaller projects may create too much

the overall complexity


For

complexity

www.bsthome.com

Struts

36

Source code example

Drag icon object to window explorer

www.bsthome.com

Struts

37

Das könnte Ihnen auch gefallen