Transcript Document
Struts 2 Development
Topics
Roles in Struts Development
Control Flow
Actions
Struts 2 Views and Target
Struts 2 Custom Tags
Validation
Form Submission
Error Handling
Resource Bundles
JSTL and JSF
Roles in Struts
Development
Web Developer
Uses Struts custom tags, Java Server pages
and sample files to create custom web apps
Uses XML, HTML, CSS, JSP and scripting
languages to customize pages
Java Developer
programs in Java to develop Controller classes
develops Java Components (Servlets,
JavaBeans, etc.) for multi-application use
The Controller
FilterDispatcher
Behaves like an Action factory
Control Flow
Client
Request
FilterDispatcher
Should an
action be
invoked?
ActionMapper
Control Flow
FilterDispatcher
ActionProxy
ActionInvocation
Look up
Action class
struts.xml
Control Flow
ActionInvocation
Look up
Result Code
Action
struts.xml
execute()
The Action Class
POJO
Can contain an instance of the model as member
variable because Action classes are instantiated
for each request (thread-safe)
Override the execute() method
public String execute()
Returns a simple String that serves as a symbolic
name to determine what should happen next
Configuring the Action
Class
Compile the new Action and put it in Web
application’s classpath
Add an <action> element to struts.xml
Several <action> elements can use the same
Action class as long as name attribute is
different
<action name=“login”
class=“edu.weber.LoginAction”>
..
</action>
Struts 2 Views
Composed of JSPs, custom tag libraries, and
ActionForm objects
JSPs
Present Data
Gather Data
Targets of Action result
JSP Targets
Action-Specific Forward
<action ..
..
<result name=“success”>Welcome.jsp
</result>
<result name=“error”>Login.jsp
</result>
</action>
Struts 2 Custom Tags
Tag Library:
<%@ taglib prefix=“s” uri=“/struts-tags” %>
<s:form/>
<s:textfield/>
<s:password/>
<s:submit/>
<s:actionerror/>
Struts 2 Custom Tags
Struts 2 utilizes FreeMarker to generate the
markup for the tags.
FreeMarker can generate any type of markup:
HTML, XSLT, WML, etc.
Validation
Validation is managed at the object level
Fields in an Action class can be checked
through configuration file:
<ActionClassName>-validation.xml
Validation Example
LoginAction-validation.xml
<!DOCTYPE validators PUBLIC
“-//OpenSymphony Group//Xwork Validator 1.0.2//EN”
http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd>
<validators>
<field name=“username”>
<field-validator type=“required”>
<message key=“required” />
</field-validator>
</field>
</validators>
Struts 2 Form Submission
Form state/data combined with Action class
<s:form action=“login.action” method=“post”>
<s:textfield name=“username”
key=“label.username” size=“20” />
</s:form>
Struts 2 Error Handling
Establish ActionError tag on view
<s:actionerror />
Use addActionError to accumulate message on
ValueStack
addActionError(getText(“error.login”));
Defining Resource
Bundles
ResourceBundle is a file which contains a
series of name/value pairs
Naming format is
ResourceBundleName.properties
For example a file named
ApplicationResources.properties might
contain the entry
error.age.invalid=Invalid Age
Deploying
ResourceBundles
Place a copy of the resource bundles in the /WEBINF/classes directory of your web application
using appropriate package directory structure
Place the following entry in struts.xml
<constant
name=“struts.custom.i18n.resources”
value=“ApplicationResources />
Retrieving Localized
Messages
Web Page: key attribute
<s:textfield name=“username”
key=“label.username” size=“20” />
Java: getText()
getText(“promptTitle”);
JSP Standard Tag Library
(JSTL)
Establish a common set of custom tags
Create syntax that eliminates scripting within Web
pages
Supports
Core operations (iteration, conditionals)
XML Processing
Internationalization formatting
Database access
The Jakarta Taglibs Standard 1.0 tag library is an
implementation of JSTL 1.0.
JavaServer Faces (JSF)
Component model for building user interfaces for
Web Applications
APIs for managing UI state, handling events
and input validation, page navigation
JSP Custom tag library
Benefits
Ease-of-Use
Standardization
Device Independence
Review
Roles in Struts Development
Control Flow
Actions
Struts 2 Views and Target
Struts 2 Custom Tags
Validation
Form Submission
Error Handling
Resource Bundles
JSTL and JSF