spring mvc4.2 ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
来源:互联网 发布:elementary os ubuntu 编辑:程序博客网 时间:2024/06/06 15:48
spring mvc配置文件:
写个测试controller
测试代码:
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/><mvc:view-controller path="/" view-name="index"/><bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"><property name="mediaTypes"><value>html=text/htmljson=application/json</value></property><property name="defaultContentType" value="text/html"/></bean><bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"><property name="order" value="0"/><property name="contentNegotiationManager" ref="contentNegotiationManager"/><property name="viewResolvers"><list><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"></property></bean></list></property><property name="defaultViews"><list><bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView"><property name="charset" value="UTF-8"/></bean></list></property></bean><bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/></beans>
写个测试controller
package com.doctor.springframework.web.view;import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView; @Controllerpublic class SimpleController {@RequestMapping({"/test.html","/test.json"})public ModelAndView test() {Map<String, String> map = new HashMap<>();map.put("test", "json");map.put("test-html", "html");map.put("what", "what");ModelAndView modelAndView = new ModelAndView("/contentViewTest");modelAndView.addObject(map);return modelAndView;}}
测试代码:
package com.doctor.springframework.web.view;import org.apache.http.client.fluent.Request;import org.apache.http.client.fluent.Response;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.doctor.embeddedjetty.EmbeddedJettyServer3;/** * ContentNegotiatingViewResolverPractice 根据路径后缀,选择不同视图 * @author doctor * * @time 2015年1月7日 上午10:08:24 */public class ContentNegotiatingViewResolverPractice2 {private EmbeddedJettyServer3 embeddedJettyServer;private int port;@Beforepublic void init() throws Throwable {port = 8989;embeddedJettyServer = new EmbeddedJettyServer3(port,"/contentNegotiatingViewResolverPractice/webapp", SpringContextConfig.class, SpringMvcConfig2.class);embeddedJettyServer.start();}@Testpublic void test() throws Throwable {Response response = Request.Get("http://localhost:8989/test.json").execute();System.out.println(response.returnContent().asString());response = Request.Get("http://localhost:8989/test.html").execute();System.out.println(response.returnContent().asString());response = Request.Get("http://localhost:8989").execute();System.out.println(response.returnContent().asString());}@Afterpublic void destroy() throws Throwable {embeddedJettyServer.stop();}}
01-10 10:18:40.513 main INFO o.e.j.s.Server -Started @4908ms{"hashMap":{"test":"json","test-html":"html","what":"what"}}<!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>hello contentNegotiatingViewResolverTest</body></html><html><body><h2>Hello World! doctor</h2></body></html>
0 0
- spring mvc4.2 ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- Spring mvc ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- Spring MVC4.1-ContentNegotiatingViewResolver
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图
- SpringMVC之ContentNegotiatingViewResolver实现根据请求资源名选择视图
- 8. Spring MVC4.1-ContentNegotiatingViewResolver
- Spring MVC根据请求后缀返回不同数据格式
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- Spring mvc 视图解析器 ContentNegotiatingViewResolver 源码分析
- spring-mvc多视图解析配置实例 ContentNegotiatingViewResolver
- 混合视图技术--ContentNegotiatingViewResolver
- Spring MVC-ContentNegotiatingViewResolver
- iOS数组内容不重复
- linux v4L2 初步框架
- java 解决simpledateformat线程安全问题
- maven学习
- Unix:用户自定义信号-SIGUSR1和SIGUSR2
- spring mvc4.2 ContentNegotiatingViewResolver 根据路径后缀,选择不同视图
- tomcat工作原理
- Linux下Tomcat版本升级方案
- ASP.NET MVC(4)
- 深入理解Java:SimpleDateFormat安全的时间格式化
- Android版手机斗地主游戏源码
- 颜色知识,ps调整色彩
- pdm工具批量生成建表语句
- perl 监控数据变化