1. Unit 7: Design Patterns and Frameworks
A framework “is an abstraction in which common code
providing generic functionality can be selectively overridden
or specialized by user code providing specific functionality.”
(from Wikipedia)
Here we are going to consider 3 MVC-based Web frameworks
for Java:
Struts 1
Spring MVC
JavaServer Faces
dsbw 2011/2012 q1 1
17. Struts 2
Struts 2 vs Struts 1 (according to struts.apache.org/2.x)
Enhanced Results - Unlike ActionForwards, Struts 2 Results can
actually help prepare the response.
Enhanced Tags - Struts2 tags don't just output data, but provide
stylesheet-driven markup, so that we consistent pages can be created
with less code.
POJO forms - No more ActionForms: we can use any JavaBean we like
or put properties directly on our Action classes.
POJO Actions - Any class can be used as an Action class. Even the
interface is optional!
First-class AJAX support - The AJAX theme gives interactive
applications a boost.
Easy-to-test Actions – Struts 2 Actions are HTTP independent and can
be tested without resorting to mock objects.
Intelligent Defaults - Most framework configuration elements have a
default value that we can set and forget.
dsbw 2011/2012 q1 17
19. Spring MVC
Spring's own implementation of the Front Controller Pattern
Flexible request mapping and handling
Full forms support
Supports several view technologies
JSP/Tiles, Velocity, FreeMarker
Support integration with other MVC frameworks
Struts, Tapestry, JavaServerFaces, WebWork
Provides a JSP Tag Library
dsbw 2011/2012 q1 19
20. Spring Framework Architecture
Spring Web
Spring ORM WebApplicationContext
Hibernate support Struts integration
iBatis support Tiles integration Spring MVC
JDO support Web utilities
Spring AOP Web MVC Framework
AOP infrastructure JSP support
Metadata support Velocity/FreeMarker
Declarative transaction support
management PFD/Excel support
Spring DAO Spring Context
Transaction Infrastructure ApplicationContext
JDBC support JNDI, EJB support
DAO support Remoting
Spring Core
IoC Container
dsbw 2011/2012 q1 20
22. Spring MVC: Terminology wrt. J2EE Core Patterns
Spring MVC J2EE Core Patterns
Concept
DispatcherServlet Front Controller /
Application Controller
HandlerMapping Command Mapper
ModelAndView View Handle /
Presentation Model
ViewResolver View Mapper
Controller Business Helper
dsbw 2011/2012 q1 22
23. Spring MVC: Setting Up
1. Add the Spring dispatcher servlet to the web.xml
2. Configure additional bean definition files in web.xml
3. Write Controller classes and configure them in a bean
definition file, typically META-INF/<appl>-servlet.xml
4. Configure view resolvers that map view names to to views
(JSP, Velocity etc.)
5. Write the JSPs or other views to render the UI
dsbw 2011/2012 q1 23
24. Spring MVC: Controllers
public class ListCustomersController implements Controller {
private CustomerService customerService;
public void setCustomerService(CustomerService
customerService)
{ this.customerService = customerService; }
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception
{
return new ModelAndView(“customerList”, “customers”,
customerService.getCustomers());
}
}
ModelAndView object is simply a combination of a named
view and a Map of objects that are introduced into the
request by the dispatcher
dsbw 2011/2012 q1 24
25. Spring MVC: Controllers (cont.)
Interface based
Do not have to extend any base classes (as in Struts)
Have option of extending helpful base classes
Multi-Action Controllers
Command Controllers
Dynamic binding of request parameters to POJO (no ActionForms)
Form Controllers
Hooks into cycle for overriding binding, validation, and inserting
reference data
Validation (including support for Commons Validation)
Wizard style controller
dsbw 2011/2012 q1 25
26. JavaServer Faces (JSF)
Sun’s “Official” Java-based Web application framework
Specifications:
JSF 1.0 (11-03-2004)
JSF 1.1 (25-05-2004)
JSF 1.2 (11-05-2006)
JSF 2.0 (28-06-2009)
Main characteristics:
UI component state management across requests
Mechanism for wiring client-generated events to server side
application code
Allow custom UI components to be easily built and re-used
A well-defined request processing lifecycle
Designed to be tooled
dsbw 2011/2012 q1 26
27. JSF: Application Architecture
Servlet Container
Client
JSF Application
Devices
Business DB
Phone
Objects
JSF Framework
PDA
Model
Objects
Laptop EJB
Container
dsbw 2011/2012 q1 27
28. JSF framework: MVC
Request Response
FacesServlet
Component Model Objects
Tree Managed JavaBeans
View
Resources Delegates
Config JavaBeans Converters
Property Files Validators
XML Renderers
Action
Handlers Business Objects
Controller & Event EJB Model
Listeners JDO
JDBC
dsbw 2011/2012 q1 28
29. JSF: Request Processing Lifecycle
Response Complete Response Complete
Request Restore
Apply Request Process Process Process
Component
Value Events Validations Events
Tree
Render Response
Response Complete Response Complete
Response Render Process Invoke Process Update Model
Response Events Application Events Values
Conversion Errors
Validation or
Conversion Errors
dsbw 2011/2012 q1 29
30. JSF: Request Processing Lifecycle
Restore Component Tree:
The requesting page’s component tree is retrieved/recreated.
Stateful information about the page (if existed) is added to the
request.
Apply Request Value:
Each component in the tree extracts its new value from the
request parameters by using its decode method.
If the conversion of the value fails, an error message associated
with the component is generated and queued .
If events have been queued during this phase, the JSF
implementation broadcasts the events to interested listeners.
Process Validations:
The JSF implementation processes all validators registered on
the components in the tree. It examines the component
attributes that specify the rules for the validation and compares
these rules to the local value stored for the component.
dsbw 2011/2012 q1 30
31. JSF: Request Processing Lifecycle
Update Model Values:
The JSF implementation walks the component tree and set the
corresponding model object properties to the components'
local values.
Only the bean properties pointed at by an input component's
value attribute are updated
Invoke Application:
Action listeners and actions are invoked
The Business Logic Tier may be called
Render Response:
Render the page and send it back to client
dsbw 2011/2012 q1 31
32. JSF: Anatomy of a UI Component
Event
Handling Render
Model
binds has
has
Id has has
Local Value UIComponent Validators
Attribute Map
has
has
Child
Converters
UIComponent
dsbw 2011/2012 q1 32
36. JSF: Managed (Model) Bean
Used to separate presentation from business logic
Based on JavaBeans
Similar to Struts ActionForm concept
Can also be registered to handle events and conversion and
validation functions
UI Component binding example:
<h:inputText id=”username”
value=”#{logonBean.username}”/>
dsbw 2011/2012 q1 36
37. What’s new in JSF 2.0?
New Page Declaration Language (PDL) based on Apache
Facelets
Custom components much easier to develop
Improved Ajax integration and support
dsbw 2011/2012 q1 37
38. References
Books:
B. Siggelkow. Jakarta Struts Cookbook. O'Reilly, 2005
J. Carnell, R. Harrop. Pro Jakarta Struts, 2nd Edition. Apress, 2004
C. Walls, R. Breidenbach. Spring in Action. Manning, 2006.
B. Dudney, J. Lehr, B. Willis, L. Mattingly. Mastering JavaServer Faces.
Willey, 2004.
Web sites:
struts.apache.org
rollerjm.free.fr/pro/Struts11.html
www.springframework.org
static.springframework.org/spring/docs/1.2.x/reference/mvc.html
java.sun.com/javaee/javaserverfaces
dsbw 2011/2012 q1 38