The last thing we need to take care of is configuring up the right view
resolver in our application context. We can't use the standard
InternalResourceViewResolver here because we're not mapping to a
URL; instead we want to map to a view (namely, the RssNewsFeedView
that we just created) and we want that view to handle generating the output
directly.
The simplest approach here is to use something called the BeanNameViewResolver.
The idea with this type of resolver is quite simple. Whenever a controller returns a
logical view name, the BeanNameViewResolver will attempt to find a bean
on the application context with the same name (or ID). If there's a match, then it
interprets that bean as the mapped view. Otherwise, the other resolvers are given
their shot at matching the view name.
To add a BeanNameViewResolver, all we need to add is this:
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
We also need to inject the view name into our controller, as we saw in listing 1. Here's how we can do that:
<bean class="rssdemo.web.NewsController"
p:newsService-ref="newsService"
p:rssNewsFeedView="rssNewsFeedView"/>
We just chose the name rssNewsFeedView more or less arbitrarily;
we could have chosen anything. It's good to choose something accurate and descriptive
just to keep things clear and the minimize the chance of a mapping conflict, since
the name you choose is going to be a bean name. Speaking of which, we'll need to
put our view on the app context too:
<bean id="rssNewsFeedView"
class="rssdemo.web.RssNewsFeedView"
p:feedTitle="Ye Olde Cigar Shoppe"
p:feedDescription="Latest and greatest news about cigars"
p:feedLink="http://yeoldecigarshoppe.com/"/>
And there you have it! When the DispatcherServlet gets a request
for the RSS feed, it will run the controller method and then grab the resulting
view name, which we've configured to be rssNewsFeedView. Then the
BeanNameViewResolver will find the corresponding view bean on the app
context—in this case our RssNewsFeedView—and the view bean
will finish up the request as required. All in all a nice, clean way of handling
feed publication.