🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
使用 ContentNegotiatingViewResolver 多视图解析器可以同时解析多种类型的视图。 <br/> 下面演示同时能够解析`.xml`、`.json`、`.jsp`视图的配置,步骤如下: **1.引入能够解析xml和json的依赖** ```xml <!-- xml视图依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>5.2.9.RELEASE</version> </dependency> <!-- json视图依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>2.9.5</version> </dependency> ``` **2. 配置ContentNegotiatingViewResolver** *`resources/springmvc-servlet.xml`* ```xml <!-- 1. 配置ContentNegotiationManagerFactoryBean --> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <!-- 开启后缀识别。如识别请求地址 http://localhost:8080/xxx/order.json 的后缀名 .json --> <property name="favorPathExtension" value="true" /> <!-- 开启请求参数识别。如识别请求地址 http://localhost:8080/xxx?format=json 中format指定的json--> <!-- format是默认参数名 --> <property name="favorParameter" value="true" /> <!-- format是默认参数名,如果想更改为format2可以如下配置,则请求地址应为 http://localhost:8080/xxx?format2=json --> <!-- <property name="parameterName" value="format2"/> --> <!-- 关闭accept头识别,默认false开启 --> <property name="ignoreAcceptHeader" value="true" /> <!-- 服务器默认的MediaType类型,不指定默认返回html页面 --> <property name="defaultContentType" value="text/html" /> <!-- 根据后缀名映射数据类型,如http://localhost:8080/xxx/order.json 就对应application/json--> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </map> </property> </bean> <!-- 2. 配置ContentNegotiatingViewResolver解析器 --> <bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <!-- 如果同时配置了多种类型视图,则根据order来觉得哪个视图先被调用,order的值越小越先被调用 --> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="defaultViews"> <list> <!-- json 视图 --> <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> <!-- xml 视图 --> <bean id="xmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="marshallerProperties"> <map> <entry key="jaxb.encoding" value="UTF-8"></entry> </map> </property> <property name="packagesToScan" value="com.learn.springmvc02.pojo" /> <!--<property name="classesToBeBound">--> <!--<list>--> <!--<value>com.learn.springmvc02.pojo.Student</value>--> <!--</list>--> <!--</property>--> </bean> </constructor-arg> </bean> </list> </property> </bean> <!-- 3. 配置JSP视图解析器,当没有找到合适的视图解析器时,因为在这里配置的order是最大的,最后则有jsp视图解析器进行解析 --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="1" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- prefix和suffix共同决定jsp文件的位置 --> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> <property name="exposeContextBeansAsAttributes" value="true" /> </bean> ``` **3. 实体类** 如果一个实体类需要xml视图显示,需要添加注解`@XmlRootElement`、`@XmlElement`。 ```java /** * 提醒:使用注解@XmlElement时,不要使用lombok的@Data注解,否则会报异常 */ @Getter @XmlRootElement(name = "teacher") public class Teacher { private String name; private String gender; private Integer total; private Date born; @XmlElement public void setName(String name) { this.name = name; } @XmlElement public void setGender(String gender) { this.gender = gender; } @XmlElement public void setTotal(Integer total) { this.total = total; } @XmlElement public void setBorn(Date born) { this.born = born; } } ``` **4. controller层** ```java @Controller public class TeacherController { @RequestMapping("/to/type/{suffix}") public String toTeacher(@PathVariable("suffix")String suffix, Model model) { Teacher teacher = new Teacher(); teacher.setName("张三"); teacher.setGender("男"); teacher.setTotal(179); teacher.setBorn(new Date()); model.addAttribute("teacher", teacher); return "teacher"; } } ``` **5. 视图层** *`webapp/WEB-INF/views/teacher.jsp`* ```html <h1>${teacher.name},${teacher.gender},${teacher.total},${teacher.born}</h1> ``` **6. 测试** (1)http://localhost:8080/mvc/to/type/teach.json 。请求地址后缀为`.json`,界面将看到如下的 json 字符串效果。 ```json {"teacher":{"name":"张三","gender":"男","total":179,"born":1654183013119}} ``` (2)http://localhost:8080/mvc/to/type/teach.xml 。请求地址后缀为`.xml`,界面将看到如下的 xml 效果。 ```xml <teacher> <born>2022-06-02T23:17:49.154+08:00</born> <gender>男</gender> <name>张三</name> <total>179</total> </teacher> ``` (3)http://localhost:8080/mvc/to/type/teach 、http://localhost:8080/mvc/to/type/teach.xlsx 。请求地址不添加后缀,或者添加了不能被解析的后缀,界面将会看到如下的 jsp 页面效果。 ```html 张三,男,179,Thu Jun 02 23:19:56 CST 2022 ```