开发者

Getting both xml and jsp view in spring

开发者 https://www.devze.com 2023-03-23 15:17 出处:网络
I am working on an app which provides views for some service and acts as web service for others. In controller i have mapped \'/\' to a view and when the app starts this view is shown.

I am working on an app which provides views for some service and acts as web service for others.

In controller i have mapped '/' to a view and when the app starts this view is shown.

Now when i am trying to use view resolver like this

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
<property name="mediaTypes">
    <map>
        <entry key="html" value="text/html" />
        <entry key="xml" value="application/xml" />
       </map>
</property>
<property name="defaultViews">
    <list>
    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
         <constructor-arg>
        <bean class="org.springframework.oxm.xstream.XStreamMarshaller"
            p:autodetectAnnotations="false" />
              </constructor-arg>
    </bean>
    </list>
    </property>
<property name="viewResolvers">
    <list>
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    </list>
    </property>  


    </bean>
    <bean id="jspView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>

But problem is now everything is coming in xml format. So when i start the application

the first page by default comes开发者_Go百科 in xml format but i want a view.


By default, ContentNegotiatingViewResolver uses the HTTP Accept request header to decide which view to render. It would only send back an XML view if the client indicated in that header that it could handle the application/xml content-type, which some browsers do.

If the browser indicates that it can accept HTML and XML, then it's unpredictable as to which you'll get back. In the absense of any other information, Spring has no way to decide which one you want.

If your URL path is just /, then you're giving it no useful information other than what the browser sends by default.

ContentNegotiatingViewResolver can be reconfigured to use a file extension in the URL to decide which view to resolve, by setting the favorPathExtension property to true. This will, for example, response to request for /blah.xml with XML, taking precedence over the Accept header.

ContentNegotiatingViewResolver has a few other options, I suggest reading the javadoc carefully.

0

精彩评论

暂无评论...
验证码 换一张
取 消