del.icio.us Digg DZone Reddit StumbleUpon
Publish an RSS Feed with Spring 3.0 - Willie Wheeler
« Previous | 1 | 2 | 3

Configure the view resolver for your RSS feed

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.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
« Previous | 1 | 2 | 3

Comments (1)

Very nice article. Thanks heaps.

By Adrian on Jul 20, 2010 at 6:58 AM PDT

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Preview:
By You
Please help us reduce comment spam:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.