STRUTS: A Mature MVC Framework for Web Applications Russell Giebelhaus Edmonton Java Users Group October 16, 2002
Introduction This presentation will Describe the Model 2 MVC pattern Demonstrate how Struts works. Show the main features of the Struts1.1-Beta2 release. Explore how implementing Struts affects • Development effort • Future site maintenance.
2
What Struts is not… A portal or multi-channel server It does not provide: Security Access Control Individual tailored custom views Data access Using Struts does not hinder implementing these features. 3
So, what is Struts? Open-Source Jakarta project Started in 2000 by Craig McClanahan Currently at Version 1.1 – Beta 2 Implementation of the Model 2 – MVC Architecture
4
The MVC Pattern MVC Pattern history In the beginning – JSPs MVC Model 1 MVC Model 2 Pattern • The motivation for the Struts Framework
5
In The Beginning - JSPs
Add user <% String error = ""; String user = request.getParameter("userName")) //is there user data from form submission if (user != null) { //...access the database and add user //...catch any errors } //check for errors if (error.length() == 0) { %> <jsp:forward page="index.jsp" /> <% }
<%=error%> Enter username to add:
In The Beginning – JSP 1.1 Clean up the JSP page 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
7
Classic MVC (Model 1) Event View
Get Model State Change Notification
Good model for Java GUIs Web challenges:
Controller
Create/Set
Model
web’s stateless behaviour view uses different technology than model or controller 8
MVC Model 2 Designed for the Web Architecture Event HTTP Request
Client Browser
Controller Servlet Create/Set
Model (JavaBean) Get
Update HTTP Response
View JSP
Provided by a backend service
9
Development Role Separation 3 distinct areas Web Application
Controller Servlet
Event HTTP Request
Client Browser
Business Layer Query/Update
Create/Set
Model JavaBean
Application Framework EJBs JDO CORBA
Get Update HTTP Response
View Layer HTML JSP
View
Style Sheets
Persistent Storage
Custom Tags 10
Struts MVC 2 Implementation Struts Components Event HTTP Request
ActionForm
Controller
Dispatch
Business Logic
ActionServlet
Client Browser
Action
Forward Forward
Creates/Sets
struts-config.xml
Resource Bundle
Update
View
Get
Model
HTTP Response
JSP
JavaBean 11
Controller Components ActionServlet Struts supplied All client requests go through here Automatically populates a JavaBean (ActionForm) with request parameters Handles Locale and multi-part form requests Determines which Action or JSP to dispatch to
12
Controller Components Action User defined Must extend org.apache.struts.action.Action Override the execute() method Acts as a coordinator to the Business Layer Gets data and determines which view to render next
13
Example Action execute method public ActionForward execute(ActionMapping mapping, ActionForm form, Exception
HttpServletRequest request, HttpServletResponse response) throws
{ //get the data for display BusinessDelegate delegate = BusinessDelegate.getInstance(); DataBean bean = delegate.getTableData(); //put the bean (model) in the request for display request.setAttribute(“tableData", bean); // Forward control to the specified success URI return (mapping.findForward("view/tabledata")); //defined in strutsconfig.xml } 14
struts-config.xml file Contains the configuration data to hook all the pieces together. Introduces a level of indirection to isolate the back end code (Actions and JSPs) Four main types of information: Message resource bundle declaration For internationalization Describes where to find the ‘application.properties’ file <message-resources parameter="resources.application"/> 15
struts-config.xml (cont’d) Global forwards These are links to other pages that are referenced in other jsps or Actions. Provides a level of indirection, so that a path change only has to updated one place. 16
struts-config.xml (cont’d) Action Mappings Defines a mapping between a logical Action name and the physical Action class (path, type) Specifies an associated ActionForm (name) Declares the input page (input) to return to if errors occur Defines local forwards (forward tag). Can also use forwards defined in the global-forwards section. 17
struts-config.xml (cont’d) Action Mappings (cont’d)
Direct Action references end in .do http://…/MySubmitAction.do
path="/MySubmitAction“ type="com.sample.form.simple.SubmitAction" scope="request" input="/pages/simpleForm.jsp"/ rel="nofollow"> name="darnSimpleFormBean" validate="true" />
18
struts-config.xml (cont’d) Bean Declarations Used by the ActionServlet Controller when it automatically populates an ActionForm Tells the Controller where to physically locate the referenced ActionForm 19
Model 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) 20
View User defined Uses the following: Java Server Pages, HTML JavaScript, StyleSheets Struts Tags and Custom Tags Resource Bundles JavaBeans (model) ActionForms
21
Struts Tag Libraries Bean – accesses javabeans and resources Logic – manages conditionals Nested – lets base tags relate to each other HTML – creates input forms Template – original page templating system Tiles – new advanced templating system
22
Bean Tags Outputs a message stored in a resource bundle Provides the mechanism for internationalization
Outputs a value from the given bean Provides formatting attribute DataBean
(reference: ‘tableData’)
int numberOfRows
b
Number of Rows: 4 23
Logic Tags Conditional statements to determine output text Examples: (empty, greaterThan, lessThan, present, iterate) Iterate allows looping over Collections
24
Logic Tags (cont’d) DataBean (reference: ‘tableData’) int numberOfRows List dataList
1
many
RowElement String columnOne String columnTwo
| |
25
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 beans themselves Lets fewer attributes be defined Person String name int age Address home
1
1
Address String city String province String postalCode 26
Nested Tags (cont’d) Before: Person...
Name:
Age:
Address...
City:
Province:
27
Nested Tags (cont’d) With Nested Tags: Person...
/>
Name:
Address...
City:
Province:
28
Html Tags Used to create input forms Tags are available for all of the HTML form input types.
29
Forms ActionForm (Form-Beans) User defined JavaBean extend org.apache.struts.action.ActionForm Need to associate one FormBean with each Action that processes form data in struts-config Automatically gets populated by controller when a form has been submitted. • Must be a direct mapping between an HTML form element name and the attribute in the ActionForm 30
HTML Tag – ActionForm Mapping Action - ActionForm relationship defined in strutsconfig.xml
AFormBean String name String number
Direct naming relationship
31
Form Validation & Error Handling 2 ways to validate the form Override validate() method in ActionForm Use the Struts-Validator package
Error Handling Struts provides easy error handling with the ActionErrors and ActionError classes.
32
Form Validation – 1st Method Override ActionForm validate() method public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); //validate the name field if ((this.name == null) || (this.name.length() < 1)) { errors.add(“inputerror", new ActionError("error.name.required.key")); } //validate the number field if ((this.number == null) || (this.number.length() < 1)) { errors.add(“inputerror", new ActionError("error.number.required.key")); } return errors; //return null or empty ActionErrors if no errors found } 33
Error Handling Use of ActionErrors class Create list of errors and add to ActionErrors Up to four parameters can be embedded in an error message. Errors are displayed in the jsp page:
Resource bundle properties determine how displayed errors look: errors.header= errors.prefix=- errors.suffix=
errors.footer=
34
Form Repopulation Struts automatically takes care of repopulating the form
35
Form Validation – 2nd Method Struts Validator package Generates both the client side (JavaScript) and Server side validation code Comes with several pre-built validation routines Required Date Length Data type Range
Credit card number Email address
Can define more routines using regular expressions 36
Duplicate Form Submission After submitting a form, the user can press the Back button and submit the same form again. In some workflows this creates problems (ie. Banking transactions) Use the Token feature in Struts to prevent this from happening
37
Duplicate Form Submission In the Action that displays form: //mark form token start saveToken(request);
In the Action the processes form data: if (!isTokenValid(request)) { // duplicate submit, return error message } //token is valid resetToken(request); //....process form....
38
Tiles A templating tag library Provides abstraction between the Components of a web page (tiles) • Header, menu, body, footer, etc.
Layout of the web page • Defines how the tiles are arranged on the page
Allows for greater reuse of tiles Allows developer to easily change the web site look and feel 39
Tiles - Components Layout Header Tile Menu Tile
Body Tile
Footer Tile 40
Tiles – Sample Layout.jsp <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
41
Tiles – Definition.xml <definition name="main.default" path="/layouts/layout.jsp"> value can be: •jsp page •another tile definition •text 42
Tiles – Extending a Definition <definition name="logon" extends="main.default">
43
Tiles - Possibilities Dynamically generate the pages Change any attributes on the fly Load different tiles according to Locale Load different tiles according to a key – make site multi-channelled (personalized)
44
Other Features in Struts File uploading Multi-part form handling not defined in J2EE spec Struts provides implementation for doing this
Dynamic ActionForms Can define ActionForms in struts-config.xml No need to create an actual bean for each form
Sub-Applications Allows for multiple struts-config.xml files Large development teams can work on same project
DataSource definition Able to define datasources in the struts-config.xml 45
Struts - Tools Xdoclet (WebDoclet subproject) Generates the struts-config.xml file from tags in the JavaDoc of the ActionForm and Action classes
Struts-config.xml editors Plugins to various IDEs (Jbuilder, Eclipse, etc)
Full project code generators From a given database schema creating the database accessors, Actions, struts-config.xml, and jsps to create, update and view the data.
Check http://jakarta.apache.org/struts/resources for current tool list 46
When Not To Use Struts Struts gives a project increased flexibility, re-usability, and division of roles. But, comes at the cost of added complexity For medium to large projects it helps to manage the overall complexity For smaller projects may create too much complexity Ask questions about the future of the project: • How much might the project grow • How open to change must it be 47
Future Directions of the Struts Project Workflow Management System Will allow controlling multiple page business processes through a central, rules-based system Commercial products currently let developers define workflow rules – the Struts workflow management will be similar
Incorporation into the JSP Standard Tag Library 48
Summary Model 2 MVC is a excellent architecture for developing robust Web applications Page oriented approach is simple, but fails as the application grows. Model 2 • Lets team members specialize their efforts • Takes care of mixing various technologies
49
Summary (cont’d) Struts provides a mature Model 2 implementation Has been developed over the past 2 years with active developer and user communities. Robust set of features Adheres to the Model 2 MVC paradigm Enjoys strong industry support
50
Resources Lots of links to documentation and samples at the Struts homepage: http://jakarta.apache.org/struts
51
Questions?