Beruflich Dokumente
Kultur Dokumente
com
www.bsthome.com
Struts
What is Struts?
Open-Source Jakarta project Started in 2000 by Craig McClanahan Currently at Version 1.1 Implementation
www.bsthome.com
Struts
Developed by Industry Experts Stable & Mature Manageable learning Curve Open Source
Feature-rich
www.bsthome.com
Internationalization(I18N) Support
Rich JSP Tag Libraries Based on JSP, Servlet, XML, and Java
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
www.bsthome.com
Struts
www.bsthome.com
Struts
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)
Get
www.bsthome.com
Struts
10
www.bsthome.com
Struts
11
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
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
CORBA, etc.) : However, one should always decouple the client application from the specific business layer
implementation
www.bsthome.com
Struts
View Component
JSP
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
www.bsthome.com
Struts
20
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.
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
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
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
occur
Defines
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
referenced ActionForm
<form-beans>
<form-bean name="testActionForm" type="test.TestActionForm" />
</form-beans>
www.bsthome.com
Struts
28
www.bsthome.com
Struts
29
Used to create input forms Tags are available for all of the HTML form input types.
www.bsthome.com
Struts
30
Outputs a message stored in a resource bundle Provides the mechanism for internationalization
<bean:write>
<bean:message key="global.title"/>
<bean:write name="tableData" property="numberOfRows" />
www.bsthome.com
Struts
31
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
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
<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
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
35
Struts gives a project increased flexibility, reBut, comes at the cost of added complexity
complexity
www.bsthome.com
Struts
36
www.bsthome.com
Struts
37