Chapter 7. Application and system design guidelines 149
Figure 7-4 Result bean and view bean design pattern
Advantages of result beans
Some reasons to use result beans are:
򐂰 Result beans clearly define what the controller expects back from the
business logic.
򐂰 Once a result bean is defined, the developers of the Controller and the Model
can develop their components independently. This simplifies and optimizes
the development process and allows for parallel development.
򐂰 Since result beans can be serialized, they can be sent to remote servers or
received from remote servers, such as EJB-based distributed applications. In
addition, they can be stored in a file for persistence purposes.
򐂰 The result bean data structure can be reused by multiple business logic
objects and interaction controller objects.
7.2.3 View bean design pattern
A view bean defines the contract between the Controller and the View. It lists all
the attributes the JSP can display. The main benefit of defining such a view bean
HTML
JavaScript
Browser Client
Web Application Server
Command Bean
Model
Page
Construction
View
Also holds the
result data
Business Logic
View
Bean
Controller
Interaction
Controller
Result
Bean
150 Patterns: Implementing Self-Service in an SOA Environment
is to make it easy for the JSP page designer to get all the required data in one
place. The display page often contains the data from the following sources:
򐂰 Result bean properties (returned by the business logic)
򐂰 HTTP request data (including attributes, parameters, cookies, URL string)
򐂰 Session state
򐂰 Servlet context
Figure 7-4 on page 149 demonstrates the relationship between MVC
components, view beans, and result beans. See 7.2.2, “Result bean design
pattern” on page 148, for our discussion on result beans.
The Controller is responsible for instantiating the view bean and initializing all of
the properties of the bean. View beans can be designed to be responsible for
view-specific transformations. For example, a view bean can be responsible for
converting the monetary values into the user-preferred currency. Such a view
bean can have two properties: The monetary value in a base currency and the
currency display type. The Controller can initialize both of these properties. The
view bean can use this information to call a reusable currency conversion library
and get the monetary value in the appropriate format.
Usually, view beans are tightly coupled with a JSP, because the primary purpose
is to provide all the properties the JSP designer would need in one place.
However, under special circumstances one can reuse the view beans by
inheritance.
Both result beans and view beans are implemented using simple Java beans. In
simple Web interactions, both a result bean and a view bean could be
implemented by the same Java bean.
Advantages and disadvantages of view beans
Some advantages of view beans are:
򐂰 Clearly defines all the fields a View (JSP) can display.
򐂰 Once a view bean is clearly defined, the developers of the Controller and the
View can develop their components independently. This simplifies and
optimizes the development process and allows for parallel development.
򐂰 The View (JSP) designer can get all the dynamic data from one view bean
and use <jsp:useBean> and <jsp:getProperty> tags to insert these values.
This allows the JSP designer to concentrate on the look and feel of the page
rather than worry about gathering data from various sources (for example,
sessions, cookies, result beans, and so on) and coding complex View-specific
Java code. View beans effectively hide these complexities from the display
page designer.

Get Patterns: Implementing Self-Service in an SOA Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.