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