How to change mediaType for a view in Controller - Spring MVC -
How to change mediaType for a view in Controller - Spring MVC -
i have implemented content negotiation using http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc.
i have controller providercontroller & views in controller appended .action in web.xml
<servlet-mapping> <servlet-name>provider</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
and if view needs rendered in html, utilize format query parameter (ex: renderhtml.action?format=html). how render page in format without using format query parameter?
p.s : using spring 3.0. can't utilize produces annotation.
thanks
edit :
viewresolver configuration in provider-servlet.xml
<bean id="viewresolver" class="org.springframework.web.servlet.view.contentnegotiatingviewresolver"> <property name="favorparameter" value="true" /> <property name="parametername" value="format" /> <property name="ignoreacceptheader" value="true" /> <property name="defaultcontenttype" value="application/json" /> <property name="favorpathextension" value="false" /> <property name="mediatypes"> <map> <entry key="json" value="application/json" /> <entry key="html" value="text/html" /> </map> </property> <property name="viewresolvers"> <list> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/provider/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> <property name="defaultviews"> <list> <bean class="org.springframework.web.servlet.view.json.mappingjacksonjsonview" /> </list> </property> </bean>
web.xml :
<servlet> <servlet-name>provider</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>\web-inf\provider-servlet.xml</param-value> </init-param> <init-param> <param-name>contextclass</param-name> <param-value>org.eclipse.virgo.web.dm.serverosgibundlexmlwebapplicationcontext</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>provider</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
controller :
//the below 1 returns token.jsp if straight opened in browswer http://localhost/provider/settoken.action?format=html @requestmapping("/provider/settoken") public string settoken(webrequest webrequest, model model, httpservletrequest request) { homecoming "token"; } //the below 1 returns json if straight opened in browswer http://localhost/provider/main.action if format=html not provided. @requestmapping("/provider/main") public string main(webrequest webrequest, model model, httpservletrequest request) { homecoming "main"; }
it says right in link, ppa (path, parameter, accept) strategy
path - you're using .action doesn't apply parameter - want move away accept header - you're left using take header in requestsmake sure ignoreacceptheader not set true in contentnegtiationviewresolverbean
think of simplest controller method
@requestmapping(value = "/post", method = requestmethod.post) public string post() { homecoming "main"; }
if send 2 ajax request different take header eg. jquery
$.ajax({ headers: { accept: "application/json" }, url: 'http://localhost:8091/post', type: 'post', contenttype: 'application/json', success: function () { console.log('success json'); }, error: function () { console.log('error'); } }); $.ajax({ headers: { accept: "text/html;charset=utf-8" }, url: 'http://localhost:8091/post', type: 'post', contenttype: 'text/html', success: function () { console.log('success'); }, error: function () { console.log('error'); } })
providing have main.jsp, , added jsonview resolver in conf
<bean class="org.springframework.web.servlet.view.contentnegotiatingviewresolver"> <property name="order" value="1"/> <property name="mediatypes"> <map> <entry key="json" value="application/json"/> </map> </property> <property name="defaultviews"> <list> <bean class="org.springframework.web.servlet.view.json.mappingjacksonjsonview"/> </list> </property> </bean>
the response 2 method json first, text/html second
spring spring-mvc content-negotiation
Comments
Post a Comment