spring mvc4.2 ContentNegotiatingViewResolver 根据路径后缀,选择不同视图

springMVC4(15)RestFul多视图混合输出 混合使用多种视图技术。在前面文章里,我们对jsp、json、xml个中视图都进行了较为详细的实例解析,但涉及到的都是单视图使用配置。在实际开发中,我们可能需要混合是使用多种视图技术。尤其是针对REST编程风格,我们可以通过一个URL、多种视图来切合REST风格的同一资源、多种表述。 现在加入我们要输出JSP、JSON、XML多种视图技术,如果使用我之前文章《springMVC4(4)json与对象 阅读详情
spring mvc配置文件:

<?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/html
				json=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;
  
@Controller
public 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;
	@Before
	public void init() throws Throwable {
		port = 8989;
		embeddedJettyServer = new EmbeddedJettyServer3(port,"/contentNegotiatingViewResolverPractice/webapp", SpringContextConfig.class, SpringMvcConfig2.class);
		embeddedJettyServer.start();
	}

	@Test
	public 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());

	}

	@After
	public 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>

Spring MVC实战精要:从DispatcherServlet到Controller分层设计 Spring MVC是Java Web开发中支撑高可用、可维护后端服务的核心框架,其本质是通过DispatcherServlet统一调度、HandlerAdapter适配执行、ViewResolver解耦渲染,构建出职责清晰的MVC契约。它解决的不是‘能否运行’的问题,而是如何在真实业务中实现参数校验自动化(@Valid)、异常处理集中化(@ControllerAdvice)、响应内容协商(ContentNegotiatingViewResolver)与跨关注点解耦。技术价值在于降低重复编码成本、提升团队协 阅读详情

相关推荐

YOLOv8中的C2f模块代码详解

在YOLOv8网络结构中,C2f模块(CSP Bottleneck with 2 Convolutions)是一个关键组件,用于实现跨阶段部分聚合

zzzyyy8的博客 3万+

spring mvc 打印HTML,Spring4 MVC ContentNegotiatingViewResolver多种输出格式实例

ContentNegotiatingViewResolver是 ViewResolver 使用所请求的媒体类型的一个实现(基于文件类型扩展,输出格式URL参数指定类型或接受报头)来选择一个合适的视图一个请求。ContentNegotiatingViewResolver本身并不解决视图,只不表示为其他的 ViewResolver,您可以配置来处理特定的视图(XML,JSON,PDF,XLS,HTML...

weixin_31110741的博客 253

相机光学(三十八)——VCM(Voice Coil Motor)音圈马达

VCM(Voice Coil Motor)音圈马达全称Voice Coil Montor,电子学里面的音圈电机,是马达的一种。因为原理和扬声器类似,所以叫音圈电机,具有高频响、高精度的特点。其主要原理是在一个永久磁场内,通过改变马达内线圈的直流电流大小,来控制弹簧片的拉伸位置,从而带动上下运动。手机摄像头广泛的使用VCM实现自动对焦功能,通过VCM可以调节镜头的位置,呈现清晰的图像。弹片式结构滚珠式结构摩擦式结构Open loop开马达Close loop闭环马达。

图像学习之旅 7223

SpringMVC中的ContentNegotiatingViewResolver配置

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34306676的博客 417

Arduino DHT11 ESP8266 在OneNet云平台上读取 温湿度

#include "ESP8266.h" #include "dht11.h" #include "SoftwareSerial.h" #define SSID "540ND" #define PASSWORD "" #define HOST_NAME "api.heclouds.com" #define DEVICE_ID "" #define HOST_PORT (80) String APIKey = " "; #define INTER

weixin_45852947的博客 2430

别再为PT100测温发愁了!手把手教你用ADS1247+STM32搞定高精度温度采集(附完整源码)

本文详细介绍了如何利用ADS1247高精度ADC与STM32微控制器构建PT100测温系统,实现±0.1℃的温度测量精度。内容涵盖硬件设计、寄存器配置、SPI通信优化及温度换算算法,特别适合工业自动化和精密仪器领域的嵌入式开发人员参考。

weixin_26897585的博客 428

spring4 注解 添加ContentNegotiatingViewResolver

创建 ContentNegotiatingViewResolver package spittr.config; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; im

love13135816的专栏 765

学习笔记( 11 ) - 解决:finalshell连接虚拟机(ubuntu)超时,主机ping不通虚拟机,主机没有VMnet8网卡, Connection refused: connect

解决:finalshell连接虚拟机(ubuntu)超时,主机ping不通虚拟机,主机没有VMnet8网卡, Connection refused: connect

烟溪彭于晏的博客 2万+

Spring4.x官方参考文档中文版——第21章 Web MVC框架(18)

21.5.4 ContentNegotiatingViewResolver(内容协商视图解析器),视图解析器的配置示例

Hermaeus Mora's Apocrypha 1024

FPGA实现PI控制

FPGA实现PI控制

m0_57319166的博客 3897

泛微E9与金蝶云星空单点登录集成实战指南

本文详细介绍了泛微E9与金蝶云星空单点登录(SSO)集成的实战指南,包括前期准备、核心代码实现、常见问题排查及安全加固建议。通过集成,企业可实现一次登录全网通行,显著提升工作效率和安全性。文章特别强调了签名生成机制和跳转URL构造的关键技巧,帮助开发者避免常见错误。

weixin_30689307的博客 535

springmvc<一>一种资源返回多种形式【ContentNegotiatingViewResolver】

restful服务中一个重要的特性就是一种资源可以有多种表现形式,在springmvc中可以使用ContentNegotiatingViewResolver这个视图解析器来实现这种方式。 描述资源的三种形式 一、使用扩展名 http://localhost:8080/test/user.xml 以xml格式呈现 http://localhost:8080/test/user.j...

weixin_34220963的博客 317

【Android 11】使用Android Studio调试系统应用之Settings移植(三):构建Settings、SettingsLib项目文件

基于android 11,本系列文章力争实用,讲解使用AS调试系统app的方法,并提供了配套源下载,供学习调试。上一篇文章[Android 11]使用Android Studio调试系统应用(二):依赖分析、android.bp、静态和动态依赖,讲解了如何搭建编译服务器,如何下载AOSP源码,如何切换到AOSP的指定分支(android-11-r39),以及如何编译、解决编译错误、运行模拟器,本文将开始讲解如何构建Settings的AS工程文件! 走过、路过,有用过的同学,记得给个赞O(∩_∩)O哈!

上周的博客 2681

SpringMVC ContentNegotiatingViewResolver -- 根据请求资源名选择视图

利用使用ContentNegotiatingViewResolver,这个视图解析器允许你用同样的内容数据来呈现不同的view。

NowOrNever 1772

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Spring MVC 混合使用多种视图技术

1.ContentNegotiatingViewResolver Spring 最重要的一个新功能就是对 REST 编程风格的支持。REST 风格的应用对资源的 URL 定义有严格的要求:一个资源对象对应唯一的 URL。在前面的例子中,我们使用不同的视图显示用户列表,如果把“用户列表”看作一个资源,那么我们希望通过一个相同的 URL 访问这个资源,而不是使用不同的 URL 访问该资源的不同视图。...

weixin_30267691的博客 401

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Spring MVC学习(4)—ViewSolvsolver视图解析器的详细介绍与使用案例

基于最新Spring 5.x,详细介绍了Spring MVC的ViewSolvsolver视图解析器组件,并提供了详细的使用案例。

终生学习践行者 6047

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Spring 梳理 - ContentNegotiatingViewResolver

ContentNegotiatingViewResolver,这个视图解析器允许你用同样的内容数据来呈现不同的view。它支持如下面描述的三种方式: 1)使用扩展名http://localhost:8080/employees/nego/Jack.xml 返回结果为XMLhttp://localhost:8080/employees/nego/Jack.json 返回结果为JSONhttp...

weixin_30535913的博客 252

SpringMVC-视图解析器ViewResolver详解

本文基于spring 5.5.2.release 前几篇文章介绍了springmvc调用Controller的处理流程,现在已经知道调用Controller之后可以得到ModelAndView对象,那么如何根据ModelAndView对象得到View对象,这就需要借助视图解析器ViewResolver了。 文章目录一、ViewResolver1、BeanNameViewResolver2、ViewResolverComposite3、InternalResourceViewResolver4、Conte.

龚厂长的博客 3344

Spring Boot Web MVC配置详解

Spring Boot Web MVC配置详解,涵盖MVC核心组件与请求处理流程、自动配置原理(WebMvcAutoConfiguration)、服务器与MVC基本配置、视图解析器(Thymeleaf/FreeMarker)、拦截器注册与执行顺序、消息转换器与静态资源处理、文件上传下载、CORS跨域配置,以及开发环境与生产环境的最佳实践建议。

hou792163605的博客 545

SpringBoot DAY06 MVC自动配置

目录 1.官网阅读 1.1ContentNegotiatingViewResolver 内容协商视图解析器 1.2 转换器和格式化器 1.3修改SpringBoot的默认配置 1.4全面接管SpringMVC 1.官网阅读 在进行项目编写前,我们还需要知道一个东西,就是SpringBoot对我们的SpringMVC还做了哪些配置,包括如何扩展,如何定制。 只有把这些都搞清楚了,我们在之后使用才会更加得心应手。途径一:源码分析,途径二:官方文档! 地址 :https://docs....

m0_47325996的博客 288

SpringMVC的内容协商视图解析学习笔记

视图解析其:ContentNegotiatingViewResolvera. 关联ViewResolver Bean列表 b. 关联ContentNegotiationManager Bean c. 解析最佳匹配View 内容协商管理器:ContentNegotiationManagera. 有ContentNegotiationConfigurer配置 b. 由ContentNegotiationManagerFactoryBean 创建 c. 关联ContentNegotiationStrateg.

yichengjie_c的博客 166

spring boot 集成springmvc视图技术

spring boot 在springmvc的视图解析器方面就默认集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在视图引擎上就已经集成自动配置的模版引擎,如下: 1. FreeMarker 2. Groovy 3. Thymeleaf 4. Velocity (deprecated in 1.4) 6. Mustache JSP技术spring boot 官方是不推荐的,原因有三: 1. 在tomcat上,jsp不能在嵌套的tomcat容器解

qq_820648091的博客 302

Spring MVC学习笔记(五)

第五章 Spring MVC起步 1,Spring将请求在调度Servelet,处理器映射(handler mapping),控制器以及视图解析器(view resolver)之间移动。 在Spring MVC中,DispatcherServlet就是前端控制器。DispatcherServlet的任务是将请求发送给Spring MVC控制器(controller). 2,搭建Spring MVC...

lufqnuli的博客 360

SpringBoot——MVC自动配置原理

SpringBoot——MVC自动配置原理 文章目录SpringBoot——MVC自动配置原理1、ContentNegotiatingViewResolver配置原理2、配置格式化转换器3、配置视图控制器4、@EnableWebMvc springboot为springmvc提供了自动配置,可以很好地用于大多数应用程序。 这些自动配置在Spring默认设置的基础上进行了扩展,可以参考springboot官方文档。 文档链接:https://docs.spring.io/spring-boot/docs/2.

万里顾一程的博客 780

SpringBoot:MVC自动配置原理

基本MVC自动装配原理

yooppa的博客 339
上一篇: springmvc + freemarker 开发环境 缓存设置
下一篇: Spring+Websocket实现消息的推送
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值