Expert Spring MVC and Web Flow

(Dana P.) #1

PARENT AND ABSTRACT BEANS


We’re declaring a parent view under the name parent-view. This isn’t used as an actual view in our applica-
tion (it doesn’t have its own URL), but instead makes use of Spring’s native bean hierarchy to impose its
values on all other beans that declare it as parent. This is similar to subclassing in your Java applications. For
the homepage bean (view), Spring will create an object by instantiating org.springframework.web.
servlet.view.JstlViewbased on the parent-view.classattribute. It will then call homepage.
setAttributesCSV("title=FlightDeals.com,season=Summer"); and then homepage.setUrl
("/WEB-INF/jsp/index.jsp");.The same will happen for listFlights and any other views we define in
this file. The home page and listFlights views that declare parent-view as their parent will now have the class
and attributes that the parent view exposed.

As we’ve already seen, static attributes are overridden by dynamic model items of the
same name, so we have introduced a way to set a default value for a couple of model attrib-
utes for all views that can be amended by any Controller. Although this concept can be used
in any Spring bean definition file, it’s a particularly useful and powerful one in the view layer,
where you often have many views that will need common values.

XmlViewResolver
Your ApplicationContextand DispatcherServletcontext files are usually written in XML using
the Spring Beans DTD file. XmlViewResolverallows you to write a view definition file using the
same familiar syntax. By default, this file is named WEB-INF/views.xml,and that’s where the
resolver will expect to find it unless you configure it to look elsewhere.
The equivalent XML definition of the preceding views.propertiesfile is displayed in
Listing 7-10.

Listing 7-10.WEB-INF/views.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="parent-view" abstract="true"
class="org.springframework.web.servlet.view.JstlView">
<property name="attributes">
<props>
<prop key="title">FlightDeals.com</prop>
<prop key="season">Summer</prop>
</props>
</property>
</bean>

<bean id="homepage" parent="parent-view">
<property name="url"

212 CHAPTER 7 ■THE VIEW LAYER

Free download pdf