Spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图

ContentNegotiatingViewResolver  实现了同一资源,多种表述。这个视图解析器允许使用同样的内容数据来呈现不同的view。

ContentNegotiatingViewResolver是根据客户提交的MimeType(如 text/html,application/xml)来跟服务端的一组viewResover的MimeType相比较,如果符合,即返回viewResover的数据.而 /user.xml,ContentNegotiatingViewResolver会首先将 .xml 根据mediaTypes属性将其转换成 application/xml,然后完成前面所说的比较.

方法一:使用扩展名

http://localhost:8080/learn/user.xml 获取xml类型数据

http://localhost:8080/learn/user.json  获取json类型数据

http://localhost:8080/learn/user  使用默认view呈现,如jsp

方法二:使用http 请求头的Accept

GET /user HTTP/1.1

Accept:application/xml


GET /user HTTP/1.1

Accept:application/json

方法三:使用参数

http://localhost:8080/learn/user?format=xml

http://localhost:8080/learn/user?format=json

示例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!-- 使用spring mvc -->
        <mvc:annotation-driven />  

	<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
	<context:component-scan base-package="com.learn" />

	<!-- 同一资源,多种表述 -->
	<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
		<!-- 是否启用扩展名支持,默认为true -->
		<property name="favorPathExtenion" value="true" />
		<!-- 是否启用参数支持,默认为true -->
		<property name="favorParameter" value="false" />
		<!-- 是否忽略掉accept header,默认为false -->
		<property name="ignoreAcceptHeader" value="true" />
		
		<!-- 扩展名到mimeType的映射 -->
		<property name="mediaTypes">
			<map>
				<!-- 例如:/user.json 中的 .json 会映射到 application/json -->
                <entry key="json" value="application/json" />
                <entry key="xml" value="application/xml" />        
            </map>
		</property>
		
		<!-- 如果所有mediaType都没匹配上,就使用defaultContentType -->
		<property name="defaultContentType" value="text/html"/>
	</bean>
	
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<!-- 解析器的执行顺序 -->
		<property name="order" value="1" />
		<property name="contentNegotiationManager" ref="contentNegotiationManager" />
		
		<property name="defaultViews">
			<list>
				<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
				<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
					<constructor-arg>
						<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
							<property name="classesToBeBound">
								<list>
									<value>com.learn.model.User</value>
								</list>
							</property>
						</bean>
					</constructor-arg>
				</bean>
			</list>
		</property>
		
	</bean>
	<!-- 上面没匹配到则会使用这个视图解析器 ,解析为jsp  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="order" value="2" />
		<property name="prefix" value="/" /> 
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>
实体类:

package com.learn.model;
import javax.xml.bind.annotation.XmlRootElement;

// 获取xml类型时,必须要添加
@XmlRootElement(name="user")
public class User {

	private String userName;
	private String age;
	private String sex;
	
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
}
Controller类:

package com.learn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.learn.model.User;

@Controller
public class Resource {
	
	@RequestMapping(value="/user")
	public String queryUser(User user, ModelMap model)
	{
		User u = user;
		u.setSex("man");
		model.addAttribute("user", u);
		
		return "user";
	}

}
jsp文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<body>
    UserName: ${requestScope.user.userName } <br/>
    Age: ${requestScope.user.age } <br/>
    Sex: ${requestScope.user.sex }
  </body>
</body>
</html>

参考文章:http://blog.csdn.net/z69183787/article/details/41654603#php

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值