混合视图技术--ContentNegotiatingViewResolver
来源:互联网 发布:java语法大全 编辑:程序博客网 时间:2024/05/19 23:02
配置:
<!-- 内容协商管理器 --> <!--1、首先检查路径扩展名(如my.pdf);2、其次检查Parameter(如my?format=pdf);3、检查Accept Header--> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <!-- 扩展名至mimeType的映射,即 /user.json => application/json --> <property name="favorPathExtension" value="true"/> <!-- 用于开启 /userinfo/123?format=json 的支持 --> <property name="favorParameter" value="true"/> <property name="parameterName" value="format"/> <!-- 是否忽略Accept Header --> <property name="ignoreAcceptHeader" value="false"/> <!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 --><property name="mediaTypes"> <value> json=application/json xml=application/xml html=text/html </value> </property><!-- 默认的content type --> <property name="defaultContentType" value="text/html"/> </bean> <!-- ========================= VIEW定义 ========================= --> <!-- 内容协商视图解析器;根据客户端的不同的请求决定不同的 view进行响应, 如 /blog/1.json /blog/1.xml --> <!-- 会自动根据解析的contentType来决定使用哪个视图解析器(默认使用整个web应用中的viewResolver) --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="0"> <!-- 内容协商管理器 用于决定media type --> <property name="contentNegotiationManager" ref="contentNegotiationManager"/> <!-- 默认视图 放在解析链最后 --> <property name="defaultViews"><list> <!--<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>--> <bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView" /><bean class="org.springframework.web.servlet.view.xml.MarshallingView" p:marshaller-ref="xmlMarshaller"> </bean> </list> </property> </bean> <bean id="xmlMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="streamDriver"> <bean class="com.thoughtworks.xstream.io.xml.StaxDriver"></bean> </property> <property name="autodetectAnnotations" ><value>true</value></property><property name="annotatedClasses"><list> <value>com.huihui.chy.demo1.model.UserModel</value> </list> </property> </bean> <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="contentType" value="text/html"/> <property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/> </bean>
Controller
package com.huihui.chy.demo1.web;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.huihui.chy.demo1.model.UserModel;@Controller@RequestMapping("/user")public class ViewController {@RequestMapping("/findUsers")public ModelAndView findUsersForJson(){ModelAndView av = new ModelAndView();List<UserModel> users = this.initData();av.addObject("dataInfo", users);return av;}private List<UserModel> initData(){UserModel u1 = new UserModel(1001,"chyou1988","123");UserModel u2 = new UserModel(1002,"mahuihui","123");List<UserModel> users = new ArrayList<UserModel>();users.add(u1);users.add(u2);return users;}}Model:
package com.huihui.chy.demo1.model;import javax.validation.constraints.NotNull;import org.hibernate.validator.constraints.NotEmpty;@XStreamAlias("user")public class UserModel {@XStreamAlias("id")private Integer id;@XStreamAlias("name")private String userName;@XStreamAlias("password")private String userPassword;public UserModel(){}public UserModel(Integer id,String userName,String userPassword){this.id = id;this.userName = userName;this.userPassword = userPassword;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPassword() {return userPassword;}public void setUserPassword(String userPassword) {this.userPassword = userPassword;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}}测试:
http://localhost:8080/chy-web/user/findUsers.xml
<list>
</list>
http://localhost:8080/chy-web/user/findUsers.json{"dataInfo":[{"id":1001,"userName":"chyou1988","userPassword":"123"},{"id":1002,"userName":"mahuihui","userPassword":"123"}]}注意:
controller中model中是个list列表,在转换为xml,根元素为<list>
解决办法:使用自定义对象包含集合,model存储自定义对象而避免存储list,使用见xstream
0 0
- 混合视图技术--ContentNegotiatingViewResolver
- 混合多种视图技术:ContentNegotiatingViewResolver
- ContentNegotiatingViewResolver - 内容协商视图解析器
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- spring mvc4.2 ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- Spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- SpringMVC之ContentNegotiatingViewResolver实现根据请求资源名选择视图
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- spring-mvc多视图解析配置实例 ContentNegotiatingViewResolver
- SpringMVC 使用ContentNegotiatingViewResolver整合多种视图解析器
- 混合分割视图
- 大图文件Plist解析
- # 那些年不得不滚动的页面
- 第二周项目1—求旱冰场造价
- 【转载】 Java线程:Callable和Future
- js 中apply方法和call方法的详解
- 混合视图技术--ContentNegotiatingViewResolver
- 求帮助关于Web 标记库的问题
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- FP树
- 新人报到
- 数据库编程总结
- RubyMine快捷键
- HDOJ 1004 Let the Balloon Rise
- 丢包工具