springboot applications.yml配置HTML视图解析器_Spring Boot实践——SpringMVC视图解析

一、注解说明

在spring-boot+spring mvc 的项目中,有些时候我们需要自己配置一些项目的设置,就会涉及到这三个,那么,他们之间有什么关系呢?

首先,@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法。

所以有以下几种使用方式:

  1. @EnableWebMvc+extends WebMvcConfigurationAdapter,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
  2. extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
  3. 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版本已过时了,用官网说的新的类替换 * */@Configurationpublic 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)
  5. Mustache

JSP技术spring boot 官方是不推荐的,原因有三:

  1. 在tomcat上,jsp不能在嵌套的tomcat容器解析即不能在打包成可执行的jar的情况下解析
  2. Jetty 嵌套的容器不支持jsp
  3. Undertow

而其他的模板引擎spring boot 都支持,并默认会到classpath的templates里面查找模板引擎,这里假如我们使用freemarker模板引擎

  1. 现在pom.xml启动freemarker模板引擎视图
org.springframework.boot spring-boot-starter-freemarker 
  1. 定义一个模板后缀是ftp,注意是在classpath的templates目录下
184d8905dd15676262a508bb369a9e26.png
  1. 在controller上返回视图路径
@Controllerpublic 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值