Announcement Announcement Module
Collapse
No announcement yet.
ContentNegotiatingViewResolver Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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?

    Thanks.

  • #2
    I have solved this by implementing my own view resolver called StaticViewResolver that looks like this:
    Code:
    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:
    Code:
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
     <property name="mediaTypes">
      <map>
      	<entry key="json" value="application/json" />
    	<entry key="xml" value="application/xml" />
      </map>
    </property>
    <property name="viewResolvers">
      <list>
     	<bean class="StaticViewResolver" p:order="1">
    		<property name="view" ref="jsonView" />
    	</bean>
    	<bean class="StaticViewResolver" p:order="2">
    		<property name="view" ref="xmlView" />
    	</bean>
    	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
    					p:order="3" />
      </list>
    </property>
    
    <property name="order" value="0" />
    </bean>
    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.

    Comment


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

      Comment


      • #4
        Re; ContentNegotiatingViewResolver

        Hi,

        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.

        Comment

        Working...
        X