Announcement Announcement Module
No announcement yet.
ContentNegotiatingViewResolver Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ContentNegotiatingViewResolver

    Hey guys,

    I am looking into Spring 3.0M2 and trying use the rest stuff together with the classical mvc part, and I am running into an issue, so I hope it's ok to ask here.

    Right now it seems like I can use ContentNegotiatingViewResolver to map an .xml extension with a view x to a certain view/bean named x. This means I have to define a xml views/beans for every xml view I want to expose...

    So, what I wish for is to have all my .xml extensions, go to a certain view. Would that be possible?


  • #2
    I have solved this by implementing my own view resolver called StaticViewResolver that looks like this:
    public class StaticViewResolver implements ViewResolver, Ordered {
    	int order =  Integer.MAX_VALUE;
    	private View view;
    	public View resolveViewName(String viewName, Locale locale) throws BeansException {
    		return view;
    and is used like this:
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
     <property name="mediaTypes">
      	<entry key="json" value="application/json" />
    	<entry key="xml" value="application/xml" />
    <property name="viewResolvers">
     	<bean class="StaticViewResolver" p:order="1">
    		<property name="view" ref="jsonView" />
    	<bean class="StaticViewResolver" p:order="2">
    		<property name="view" ref="xmlView" />
    	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
    					p:order="3" />
    <property name="order" value="0" />
    In this way I manage to get the the view based on the file extension. E.g customer.json uses the json view, customer.xml uses the xml view.


    • #3
      Thanks for this tidbit, I was looking to do something like this and this is quite elegant! I learn something new everyday!


      • #4
        Re; ContentNegotiatingViewResolver


        Can you please provide me an info related to how to write xml view at server side and how to access this xml page at client side using unmarshaling.

        Thank you.