混合视图技术--ContentNegotiatingViewResolver

配置:

    <!-- 内容协商管理器  -->
    <!--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>
<user>
<id> 1001 </id>
<name> chyou1988 </name>
<password> 123 </password>
</user>
<user>
<id> 1002 </id>
<name> mahuihui </name>
<password> 123 </password>
</user>
</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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值