STRUTS
Part of the Jakarta Project Sponsored by the Apache Software Foundation Developed by: Roger W Barnes of Project Refinery, Inc.
Introduction
Project Refinery, Inc.
1
STRUTS
Objectives
Course Overview Unit 1 - Model-View-Controller Design Pattern Unit 2 - Model Components Unit 3 - View Components Unit 4 - Controller Components Unit 5 - Tag Libraries
Project Refinery, Inc. 2
STRUTS
Objectives
Unit 6 - STRUTS Configuration File Unit 7 - Web Application Descriptor File Unit 8 - Application Resources File Unit 9 – Resources
Project Refinery, Inc.
3
Model-View-Controller Design Pattern
Unit 1
Project Refinery, Inc.
4
STRUTS
MVC Design Pattern
Central controller mediates application flow Controller delegates to appropriate handler Handlers are tied to model components Model encapsulates business logic Control forwarded back through the Controller to the appropriate View
Project Refinery, Inc. 5
STRUTS
MVC Design Pattern
Project Refinery, Inc.
6
STRUTS
MVC Design Pattern
3 Major Components in STRUTS
Servlet controller (Controller) Java Server Pages (View) Application Business Logic (Model)
Controller bundles and routes HTTP request to other objects in framework Controller parses configuration file
Project Refinery, Inc.
7
STRUTS
MVC Design Pattern
Configuration file contains action mappings (determines navigation) Controller uses mappings to turn HTTP requests into application actions Mapping must specify
A request path Object type to act upon the request
Project Refinery, Inc.
8
Model Components
Unit 2
Project Refinery, Inc.
9
STRUTS
Model Components
Model divided into concepts
Internal state of the system Actions that can change that state
Internal state of system represented by
JavaBeans Enterprise JavaBeans
Project Refinery, Inc.
10
STRUTS
Model Components
JavaBeans and Scope
Page – visible within a single JSP page, for the lifetime of the current request Request – visible within a single JSP page, as well as to any page or servlet that is included in this page, or forwarded to by this page Session – visible to all JSP pages and servlets that participate in a particular user session, across one or more requests Application - visible to all JSP pages and servlets that are part of a web application
Project Refinery, Inc.
11
STRUTS
Model Components
ActionForm Beans
Extends the ActionForm class Create one for each input form in the application If defined in the ActionMapping configuration file, the Controller Servlet will perform the following:
Check session for instance of bean of appropriate class If no session bean exists, one is created automatically For every request parameter whose name corresponds to the name of a property in the bean, the corresponding setter method will be called The updated ActionForm bean will be passed to the Action Class perform() method when it is called, making these values immediately available
Project Refinery, Inc. 12
STRUTS
Model Components
When coding ActionForm beans consider:
The ActionForm class itself requires no specific methods to be implemented. It is used to identify the role these particular beans play in the overall architecture. Typically, an ActionForm bean will have only property getter and property setter methods, with no business logic The ActionForm object also offers a standard validation mechanism. If you override a "stub" method, and provide error messages in the standard application resource, Struts will automatically validate the input from the form
Project Refinery, Inc. 13
STRUTS
Continued
Model Components
Define a property (with associated getXxx() and setXxx() methods) for each field that is present in the form. The field name and property name must match according to the usual JavaBeans conventions Place a bean instance on your form, and use nested property references. For example, you have a "customer" bean on your Action Form, and then refer to the property "customer.name" in your JSP view. This would correspond to the methods customer.getName() and customer.setName(string Name) on your customer bean
Project Refinery, Inc. 14
STRUTS
Model Components
System State Beans Actual state of a system is normally represented as a set of one or more JavaBeans classes, whose properties define the current state A shopping cart system, for example, will include a bean that represents the cart being maintained for each individual shopper, and will (among other things) include the set of items that the shopper has currently selected for purchase
Project Refinery, Inc.
15
STRUTS
Model Components
Business Logic Beans Should encapsulate the functional logic of your application as method calls on JavaBeans designed for this purpose For maximum code re-use, business logic beans should be designed and implemented so that they do not know they are being executed in a web application environment For small to medium sized applications, business logic beans might be ordinary JavaBeans that interact with system state beans passed as arguments, or ordinary JavaBeans that access a database using JDBC calls
Project Refinery, Inc. 16
STRUTS
Model Components
Business Logic Beans - Continued For larger applications, these beans will often be stateful or stateless Enterprise JavaBeans (EJBs)
Project Refinery, Inc.
17
STRUTS
Model Components
Accessing Relational Databases Struts can define the datasources for an application from within its standard configuration file A simple JDBC connection pool is also provided
Project Refinery, Inc.
18
View Components
Unit 3
Project Refinery, Inc.
19
STRUTS
View Components
Internationalized Messages Struts builds upon Java platform to provide assistance for building internationalized and localized applications
Locale - fundamental Java class that supports internationalization ResourceBundle - supports messages in multiple languages PropertyResourceBundle - standard implementation of ResourceBundle that allows you to define resources using the same "name=value" syntax used to initialize properties files MessageFormat - allows you to replace portions of a message string with arguments specified at run time MessageResources - lets you treat a set of resource bundles like a database, and allows you to request a particular message string for a particular Locale
Project Refinery, Inc. 20
STRUTS
View Components
ApplicationResources.properties
Contains the messages in the default language for your server. If your default language is English, you might have an entry like this: prompt.hello=Hello
ApplicationResources_xx.properties
Contains the same messages in the language whose ISO language code is "xx"
Project Refinery, Inc.
21
STRUTS
View Components
Forms and FormBean interactions
HTML Forms and their limitations Errors not easily handled
Project Refinery, Inc.
22
STRUTS
View Components
<%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
| | |
| |
Project Refinery, Inc.
23
STRUTS
View Components
Building Forms with Struts
The taglib directive tells the JSP page compiler where to find the tag library descriptor for the Struts tag library message tag is used to look up internationalized message strings from a MessageResources object containing all the resources for this application The errors tag displays any error messages that have been stored by a business logic component, or nothing if no errors have been stored
Project Refinery, Inc.
24
STRUTS
View Components
Building Forms with Struts – continued
The form tag renders an HTML