struts overview
TRANSCRIPT
Struts Overview
Shakeel [email protected]
What is Struts Framework to develop web
applications Open source Jakarta project http://jakarta.apache.org/struts/ Current release at 1.1 Beta 3 Based on MVC-Model 2 pattern
What is MVC-Model 2
Browser
Controller
Servlet
Model
JavaBean
View
JSPHTML Stylesheets
Form submitHTTP Event
HTTP ResponseGet property
Forward
Create/Set property
BusinessLayer
What is Struts MVC-Model 2
Browser
Controller
ActionServlet
Model
JavaBeanView
JSP
BusinessLayerActionForm
Struts-Config.
xml
BusinessLogic
Action
ResourceBundle
create
Http Event
Http Response
forward
dispatch
create/set
get
forward
What is ActionServlet Traffic cop Part of the Struts Framework Consults struts-config.xml to
determine Action – ActionForm mapping
Transforms request parameters into ActionForm Type safety, validation
What is Action Extend org.apache.struts.action.Action
Override execute() Talk to your business layer to
perform the action Populates data in request/session
scope Tells Struts which page to display
return mapping.findForward(“displayResults”);
What is struts-config.xml Similar to web.xml which
configures logical names for Servlets and specifies the Java class name
Configures action mappings, form beans, forwards (views), resource bundles, plugins
Resource Bundles Configures message resource bundles
<message-resources parameter=“myApplication”/>
Utilized to NLS enable application All static strings should be specified
in myApplication.properties file Format: key=value
Name.label=Enter first name
Global forwards Remember RequestDispatcher.forward()
method – hence the name forward!!! Logical name for all global filenames Level of indirection NO FILENAMES in execute()
<forward name=“displayResults”path=“/pages/default.jsp”/>
Action Mappings Maps a form name to an Action Every form has an ActionForm Associates Java class name to an
Action Associates an input page to an Action
Page that triggered this action Used for handling validation errors
Associates result pages (forwards) to an Action
Action Mapping<action path=“/createUser”
type=“… Java class name …”scope=“request | session | application”input=“another action or jsp”name=“form bean name”>
<forward name=“…” path=“…”/><forward name=“…” path=“…”/>
</action>
Utilized bymapping.
findForward()
/createUser.do
Form Beans Instantiated by ActionServlet
before invoking Action.execute() How does ActionServlet know
which form bean? How does it know which Java class?
Struts-config.xml<form-bean name=“…”
type=“… Java class name …”/>
Form Bean Instantiation ActionServlet checks the appropriate
scope for the form bean before calling class.newInstance()
Populates data from the request parameters and does type conversion
Check out BeanUtils in Jakarta Commons project
How to write Form Beans Zero argument constructor Public getters/setters DO NOT USE ENTITY BEANS as form
beans Use Data Transfer Objects (Value
Objects) Decouple web app from business
layer
View JSP or HTML pages Use Struts tag library, JSTL tag
library DO NOT HARD CODE labels
Utilize ResourceBundle Uses beans stored in request or
session or application scope
Simple View<html:form action=“… form bean name …”>
<bean:message key=“name.label”/><html:text property=“name”/><html:submit>
…Name of the button…</html:submit>
</html:form>
Validation Override validate() in ActionForm
public ActionErrors validate(ActionMapping map, HttpServletRequest req) {ActionErrors errors = new ActionErrors();if (name==null || name.length() < 1)
errors.add(“nameMissing”, new ActionError(“msg.key”));return errors;
}
Add the following to Input page<html:errors property=“nameMissing”/>
Validator package Automatically generate client side
JavaScript code Specify WEB-INF/validation.xml
Define regular expressions for validating data, phone#, social security#, etc.
For each form specify required fields Augment rules in
WEB-INF/validator-rules.xml
No need to write ActionForm Dynamically generate ActionForm
class for your forms Specify form fields in struts-
config.xml Basic idea is properties of a bean
are a HashMap Again check Jakarta Commons
Project
Modular Struts Typical web app development
consists of 4-5 programmers Struts-config.xml is the bottleneck Create Modules (or folders) with
their own struts-config.xml
Tiles Templates for Web pages Layout various tiles of a web page
Header, Navigation, Body, Footer Specified in tiles-definition.xml
Specifies the layout Logical names for various tiles Associate filenames to the tiles
How to develop Struts apps Use WSAD 5.0