Spring Boot实践——SpringMVC视图解析
一、注解说明
在spring-boot+spring mvc 的项目中,有些时候我们需要自己配置一些项目的设置,就会涉及到这三个,那么,他们之间有什么关系呢?
首先,@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法。
所以有以下几种使用方式:
@EnableWebMvc+extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式依旧使用springboot的@EnableAutoConfiguration中的设置
具体哪种方法适合,看个人对于项目的需求和要把控的程度
在WebMvcConfigurationSupport(@EnableWebMvc)和@EnableAutoConfiguration这两种方式都有一些默认的设定
而WebMvcConfigurationAdapter则是一个abstract class
具体如何类内如何进行个性化的设置,可以参考以下文章:
Spring Boot:定制HTTP消息转换器
EnableWebMvc官方文档
- 使用 @EnableWebMvc 注解,需要以编程的方式指定视图文件相关配置
/**
* Web MVC 配置适配器
* @ClassName: WebAppConfigurer
* @Description:
* @author OnlyMate
* @Date 2018年8月28日 下午2:39:31
*
* WebAppConfigurer extends WebMvcConfigurerAdapter 在Spring Boot2.0版本已过时了,用官网说的新的类替换
*
*/
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer { /**
* springmvc视图解析
* @Title: viewResolver
* @Description: TODO
* @Date 2018年8月28日 下午4:46:07
* @author OnlyMate
* @return
*/
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
// viewResolver.setViewClass(JstlView.class); // 这个属性通常并不需要手动配置,高版本的Spring会自动检测
return viewResolver;
} /**
* SpringBoot设置首页
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
} }
- 使用 @EnableAutoConfiguration 注解,会读取 application.properties 或 application.yml 文件中的配置
## 视图
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
或
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
messages:
encoding: UTF-8
profiles:
active: dev
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
二、模版引擎
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容器解析即不能在打包成可执行的jar的情况下解析
2. Jetty 嵌套的容器不支持jsp
3. Undertow
而其他的模版引擎spring boot 都支持,并默认会到classpath的templates里面查找模版引擎,这里假如我们使用freemarker模版引擎
1.现在pom.xml启动freemarker模版引擎视图
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.定义一个模版后缀是ftp,注意是在classpath的templates目录下

3.在controller上返回视图路径
@Controller
public class HelloWorldController {
private Logger logger = LoggerFactory.getLogger(HelloWorldController.class); @Value("${question}")
private String question;
@Value("${answer}")
private String answer;
@Value("${content}")
private String content; @ResponseBody
@RequestMapping("/hello")
public String index() {
return "hello world";
} @ResponseBody
@RequestMapping(value="/hello1")
public String index1() {
return question + answer;
} @ResponseBody
@RequestMapping(value="/hello2")
public String index2() {
return content;
} @RequestMapping(value="/hello3")
public String index3(Model model) {
try {
model.addAttribute("question", question);
model.addAttribute("answer", answer);
} catch (Exception e) {
logger.info("HelloWorldController ==> index3 method: error", e);
}
return "/hello";
}
}
如果使用@RestController,默认就会在每个方法上加上@Responsebody,方法返回值会直接被httpmessageconverter转化,如果想直接返回视图,需要直接指定modelAndView。
public class HelloWorldController{
private Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
@Value("${question}")
private String question;
@Value("${answer}")
private String answer;
@Value("${content}")
private String content;
@RequestMapping("/hello3")
public ModelAndView hello3() {
try {
model.addAttribute("question", question);
model.addAttribute("answer", answer);
} catch (Exception e) {
logger.info("HelloWorldController ==> index3 method: error", e);
}
return new ModelAndView("hello");
}
}
4.配置首页
/**
* Web MVC 配置适配器
* @ClassName: WebAppConfigurer
* @Description:
* @author OnlyMate
* @Date 2018年8月28日 下午2:39:31
*
* WebAppConfigurer extends WebMvcConfigurerAdapter 在Spring Boot2.0版本已过时了,用官网说的新的类替换
*
*/
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {/**
* SpringBoot设置首页
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
} }
5.访问 http://localhost:8088/springboot


虽然,jsp技术,spring boot 官方不推荐,但考虑到是常用的技术,这里也来集成下jsp技术
1.首先,需要在你项目上加上webapp标准的web项目文件夹

2.配置jsp的前缀和后缀
参考头部 ‘注解说明’
3.在controller中返回视图
同freemaker的第三步
4.配置首页
同freemaker的第四步
5.访问 http://localhost:8088/springboot


注意
如果出现freemarker模版引擎和jsp技术同时存在的话,springmvc会根据解析器的优先级来返回具体的视图,默认,FreeMarkerViewResolver的优先级大于InternalResourceViewResolver的优先级,所以同时存在的话,会返回freemarker视图

Spring Boot实践——SpringMVC视图解析的更多相关文章
- SpringMVC视图解析器
SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视 图解析器.当我们对SpringMVC控制的资源发起 ...
- SpringMVC视图解析器(转)
前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请求时,这些请求都会被Sprin ...
- spring mvc: 资源绑定视图解析器(不推荐)
spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...
- spring mvc:内部资源视图解析器2(注解实现)@Controller/@RequestMapping
spring mvc:内部资源视图解析器2(注解实现) @Controller/@RequestMapping 访问地址: http://localhost:8080/guga2/hello/goo ...
- spring mvc:内部资源视图解析器(注解实现)@Controller/@RequestMapping
spring mvc:内部资源视图解析器(注解实现)@Controller/@RequestMapping 项目访问地址: http://localhost:8080/guga2/hello/prin ...
- Spring MVC资源绑定视图解析器
ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称. 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewRes ...
- SpringMVC 视图解析器
SpringMVC 视图解析器 还记得SpringMVC 快速入门中,dispatcher-servlet.xml 配置的视图解析器么.它是SpringMVC 的核心知识点.本章节比较简单,明白视图解 ...
- Spring Boot实践——Spring AOP实现之动态代理
Spring AOP 介绍 AOP的介绍可以查看 Spring Boot实践——AOP实现 与AspectJ的静态代理不同,Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改 ...
- Spring Boot实践——AOP实现
借鉴:http://www.cnblogs.com/xrq730/p/4919025.html https://blog.csdn.net/zhaokejin521/article/detai ...
随机推荐
- 每天一个linux命令:【转载】rmdir命令
今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...
- SpringBoot 项目修改html后不需要重新启动(热部署)
基于IDEA配置: 一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- BestCoder Round #1 第一题 逃生
// 等了好久,BESTCODER 终于出来了..像咋这样的毕业的人..就是去凑凑热闹// 弱校搞acm真是难,不过还是怪自己不够努力// 第一题是明显的拓扑排序,加了了个字典序限制而已// 用优先队 ...
- 《selenium2 python 自动化测试实战》(16)——js操作补充
js修改readonly属性 我们看到这里日期框标签中有readonly属性,如果我们直接send_keys就无法输入内容,这时我们需要先去掉readonly属性: js ='document.get ...
- Python编码规范和Python风格规范
一.原因 1.长期的工作中,发现大多数程序员的代码可读性差 2.不同的程序员之间的协作很重要,代码可读性必须很好 3.版本升级时,要基于源码升级 4.不友好的代码会影响python的执行效率 二.基于 ...
- 用Python做图像处理
转自:http://blog.csdn.net/gzlaiyonghao/article/details/1852726 最近在做一件比较 evil 的事情——验证码识别,以此来学习一些新的技能.因 ...
- autoconf 添加三方库(libcurl)简单试用
1. 参考项目 https://github.com/rongfengliang/autoconf-project 2. 项目说明 a. 项目结构 ├── Jenkinsfile # jenkins ...
- git仓库
关于仓库,我们先搞清楚三个概念:本地仓库.远程仓库和上游仓库.本地仓库是从远程仓库clone出来的,远程仓库可以从上游仓库fork出来.这里的clone和fork都是复制的意思,区别是本地和远程都是针 ...
- WCF引用方式之IIS方式寄宿服务
通过IIS方式寄宿服务 之前的例子是将控制台作为WCF的寄宿方式或者是直接添加契约项目的引用,然后通过配置或者是ChannelFactory的形式进行创建服务对象,其实在大多的开发中以IIS的形式创建 ...
- oracle版本及字符集查询
查询oracle版本:select * from v$version; 查询oracle字符集:select * from nls_database_parameters;