SpingMVC的内容协商支持三种方式:

  • 使用后缀,如jsonxml后缀和处理类型的关系可以自己定义
  • 前面说的使用Accept
  • 在访问时request请求的参数,比如每次请求request都会加format=xml参数,表示要求返回XML格式数据,默认参数名是format,可以修改。

SpingMVC规定,如果同时开启了上面的部分或全部方式,解析顺序是后缀参数Accept头。对我来说,还是比较喜欢用Accept头,用的时间长,比较适应。

SpingMVC文件中的配置方式如下:

<!-- Make this available across all of Spring MVC -->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <!-- Total customization - see below for explanation. -->
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="false" />
<property name="parameterName" value="format" />
<property name="ignoreAcceptHeader" value="false"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="text/html" /> <property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
  • favorPathExtension参数表示是否开启后缀,默认true。(使用形如/account/a.json/account/a.xml的方式)
  • favorParameter参数表示是否开启request参数识别,默认false。(使用形如/account/a?format=json/account/?format=xml的方式)
  • parameterName参数表示使用参数的名字,默认format,如果配置为mediaType,则请求格式变为/account/a?mediaType=json
  • ignoreAcceptHeader表示是否关闭accept头识别,默认false,即默认开启accept头识别。
  • defaultContentType表示服务器默认的MediaType类型。
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0"/>
<property name="contentNegotiationManager" ref="contentNegotiationManager"/><!-- 如已配置过管理器,这里可以不配置 -->
<property name="viewResolvers"><!-- 这里也可以配置viewResolver -->
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<!-- 不加配置返回 {"account":{"username":"admin","password":"123456"}} -->
<!-- 加配置返回 {"username":"admin","password":"123456"}-->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="extractValueFromSingleKeyModel" value="true" />
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
</property>
</bean>
</list>
</property>
</bean>

Spring4.1之后提供了视图解析器标签,可以用如下方式

<mvc:view-resolvers>
<mvc:content-negotiation>
<mvc:default-views>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="jsonpParameterNames">
<set>
<value>jsonp</value>
<value>callback</value>
</set>
</property>
<!-- 不加配置返回 {"account":{"username":"admin","password":"123456"}}
加配置返回 {"username":"admin","password":"123456"} -->
<property name="extractValueFromSingleKeyModel" value="true" />
</bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<!-- 启用annotation -->
<property name="autodetectAnnotations" value="true" />
</bean>
</property>
</bean>
</mvc:default-views>
</mvc:content-negotiation>
<mvc:jsp prefix="/WEB-INF/views/" suffix=".jsp" />
</mvc:view-resolvers>

mvc:content-negotiation用于定义内容协商的视图解析器,且内部可以定义默认视图;然后我们又定义了mvc:velocitymvc:groovy两个视图解析器;它们会按照顺序进行解析。另外几个视图解析器是:

  • mvc:freemarker
  • mvc:bean-name
  • mvc:jsp
@Controller
public class AccountController { /**
* <p>.json结尾返回json</p>
* <p>.xml结尾返回xml</p>
*/
@GetMapping(value="viewResolver")
public ModelAndView viewResolver(){
ModelAndView mv = new ModelAndView();
Account account = new Account(WebUtil.getRequest().getParameter("username"),
WebUtil.getRequest().getParameter("password"));
mv.addObject(account);
mv.setViewName("viewResolver");
return mv;
}
@GetMapping(value="viewResolver2")
public String viewResolver2(Model model){
Account account = new Account(WebUtil.getRequest().getParameter("username"),
WebUtil.getRequest().getParameter("password"));
model.addAttribute(account);
return "viewResolver";
} @XStreamAlias("account")
public static class Account{ private String username; private String password; public Account(){} public Account(String username, String password) {
super();
this.username = username;
this.password = password;
}
//getter setter @Override
public String toString() {
return "account:{"+username+"|"+password+"}";
} } }

最新文章

  1. 转:OSGI 实战 Equinox
  2. Nunit-Writing Tests
  3. 返回记录结构时,如果需要返回为nil时 应该怎么办。
  4. 安全性之DDOS的防护技巧
  5. Dos del参数与作用(/f/s/q)
  6. [Unity2D]精灵
  7. URL重写以后发布到IIS找不到页面
  8. 解决ld: warning: directory not found for option警告
  9. A题 - A + B Problem
  10. PHP框架、库和软件资源大全(整理篇)
  11. Hive学习笔记【转载】
  12. hdu 5584 LCM Walk(数学推导公式,规律)
  13. ThinkPHP - 模板使用函数
  14. 用Java 实现一个表中的数据复制到另一个表中
  15. hdu 2243 考研路茫茫——单词情结(AC自动+矩阵)
  16. Python爬虫入门教程 4-100 美空网未登录图片爬取
  17. RC terms.
  18. Xamarin.Forms 未能找到路径“x:\platforms”的一部分
  19. 【BZOJ5305】[HAOI2018]苹果树(组合计数)
  20. kali 局域网嗅探

热门文章

  1. Some ML Tutorials
  2. P3377 【模板】左偏树(可并堆) 左偏树浅谈
  3. springboot2.0入门(三)----定义编程风格+jackjson使用+postMan测试
  4. Zookeeper:Zookeeper集群概要
  5. 秒的换算:ms(毫秒),μs(微秒),ns(纳秒),ps(皮秒)
  6. [Linux]虚拟机无法安装deepin15.9的解决方案
  7. JDK快捷安装,以及详细安装步骤
  8. LVS集群之DR模式
  9. ACM之路(13)—— 树型dp
  10. 什么是跨平台性?原理是什么?JVM