Expert Spring MVC and Web Flow

(Dana P.) #1
Now it is simply a matter of defining the RedirectViewinstance in views.xml (as shown
in Listing 6-50). The bean’s name will match the view name the controller is using (i.e.,
newPersonSuccess). We are setting contextRelativeto trueso that the RedirectViewwill
prepend the context path name for the web application to the URL value.

Listing 6-50.views.xml
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="newPersonSuccess"
class="org.springframework.web.servlet.view.RedirectView">
<property name="contextRelative" value="true" />
<property name="url" value="/personSuccess" />
</bean>

</beans>

One of the benefits of the RedirectViewis that it will convert each object from the model
and place them in the query string of the full URL. However, for this to work correctly, those
objects are converted to Strings. Therefore, if you will be using the RedirectViewwith objects
in the model, ensure that the next page expects only Strings from the model. We will now
slightly modify our Controller(as shown in Listing 6-51), explicitly adding each property we
will need for the success page.

Listing 6-51.Modified onSubmit Using Only Strings in the Model

Map<String, Object> model = new HashMap<String, Object>();
model.put("suggestedBook",
suggestBook(person.getFavoriteProgrammingLanguage()));
model.put("personName", person.getName());
model.put("personFavoriteProgrammingLanguage",
person.getFavoriteProgrammingLanguage());

Our success page’s message (shown in Listing 6-52) will now be changed to reference
the new model properties. Notice how we are accessing the model objects from the request
parameters (as indicated by the paramprefix). This is because the objects in the model are sent
to the next page via the query string, such as /personSuccess?personName=joe&suggestedBook=
titleautomatically by the RedirectView.

Listing 6-52.New Success Page Text
Welcome, ${param.personName}! You chose ${param.personFavoriteProgrammingLanguage}
as your favorite programming language. Therefore, we can recommend you
check out ${param.suggestedBook}.

166 CHAPTER 6 ■THE CONTROLLER MENAGERIE

Free download pdf