Expert Spring MVC and Web Flow

(Dana P.) #1
By default, this will have the value text/html; charset=ISO-8859-1, and this is appropri-
ate for any view rendering HTML with a Latin character set. The value of the contentType
attribute will be used to set the appropriate HTTP headers in the response stream. This indi-
cates to the client device how it should respond. Setting the contentTypeto a binary MIME
type, for example, is likely to cause your browser to pop up a dialog box asking if you want to
save the file (or possibly launch it if your machine has an application registered to handle that
particular MIME type). Figure 7-3 shows just such an example of a content type that was set in
the HTTP response of x-application/pdf.

Figure 7-3.Browser response to a different content type header value

AbstractViewoffers the ability to set static attributes on your Viewinstance too. These attrib-
utes are independent of the dynamic model generated by the Controllers,and you can set them
programmatically or as part of the view configuration. They are useful for including additional
data in the view that you don’t want to hard-code into, for example, your JSPs or Velocity tem-
plates. AbstractView’s second contribution to the implementation of your views is the option
of declaring a requestContextAttributename. Setting a value for this will expose the Spring
RequestContextobject to your view under the name you specified. The RequestContextholds
request-specific information such as the theme, locale, binding errors, and localized messages.
Support for command and error binding in Velocity and FreeMarker views is based upon expo-
sure of the RequestContext.
Listings 7-3 and 7-4 show how you can use static attributes.

Listing 7-3.Setting Static Attributes

View view = new InternalResourceView("path");
view.addAttribute("companyName", getThisYearsCompanyName());
view.addAttribute("season", "Summer");

Listing 7-4.Using Attributes in a JSTL View

<p>The name of our company (this year at least) is:
${companyName}</p>
<p>Welcome to our ${season} season of products</p>

206 CHAPTER 7 ■THE VIEW LAYER

Free download pdf