spring mvc html模板引擎,【Spring4 MVC】(二)-配置模板引擎

引言:上一篇文章中,我们的模板引擎使用的是Thymeleaf(Spring-Boot自动配置的)。如果我想使用自己喜欢的模板引擎(比如FreeMarker、JSTL 等)应该如何做呢?

本篇文章将会在上一篇的基础上,配置一个适合自己的模板引擎(依旧以Thymeleaf为例)。

文件结构

既然是个web项目,那就赋予该项目相应的web项目结构,我在此新建了webapp目录,将resources下的html文件进行拷贝。

56cdce6af2d0

文件结构.png

编辑WebMvcConfig

新增class,WebMvcConfig.class

package com.practice;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.ViewResolver;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.thymeleaf.spring4.SpringTemplateEngine;

import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import org.thymeleaf.templateresolver.TemplateResolver;

/**

* 该配置类 类似于mvc配置文件:spring_mvc.xml

* Created by SXY on 2016/1/19.

*/

// @EnableWebMvc 用来导入mvc框架的自动化配置,使用前提是该类有@Configuration存在

/*@ComponentScan 扫描控制器组件,使用方式有两种:

* 1.指定具体类 例如@ComponentScan(basePackageClasses = HelloController.class),或者 basePackageClasses = {HelloController.class,xxx.class,…}

* 2.指定基础包 例如本例使用的方法,或者数组形式@ComponentScan({"com.*.**","com.*.**"})

* 注:basePackages 关键字 可以选择性添加,默认会自动匹配到basePackages

* */

@Configuration

@EnableWebMvc

@ComponentScan(basePackages = "com.practice")

public class WebMvcConfig {

// 声明页面的编码格式、类型

private static final String CONTENTTYPE = "text/html; charset=UTF-8";

// Thymeleaf框架配置

@Bean

public TemplateResolver templateResolver(){

ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();

resolver.setPrefix("/WEB-INF/views/");

resolver.setSuffix(".html");

// 去除缓存

resolver.setCacheable(false);

resolver.setCharacterEncoding("UTF-8");

return resolver;

}

@Bean

public SpringTemplateEngine templateEngine(){

SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();

springTemplateEngine.setTemplateResolver(templateResolver());

return springTemplateEngine;

}

/**

* 模板引擎解释器

* @return

*/

@Bean

public ViewResolver viewResolver() {

ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();

viewResolver.setContentType(CONTENTTYPE);

viewResolver.setTemplateEngine(templateEngine());

viewResolver.setOrder(1);

return viewResolver;

}

}

编辑Application

package com.practice;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;

/**

* Created by SXY on 2016/1/18.

*/

/* @SpringBootApplication 包含了:

* 1、@Configuration 该类是一个配置文件

* 2、@EnableAutoConfiguration 告诉spring-boot 进行自动化配置,加载基础包

* 3、@ComponentScan 自动扫描当前包下的class,完成mvc配置

* exclude 关键字,用来排除不需要的配置。比如我们要自定义 模板引擎,原有的就可以排除掉

* */

@SpringBootApplication(exclude = {ThymeleafAutoConfiguration.class})

public class Application {

// main方法作为程序入口,启动spring程序

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

测试

再次运行:

mvn spring-boot:run

56cdce6af2d0

测试成功.png

小结

在配置自己喜欢的模板引擎,记得添加相关依赖(pom.xml)

这里是官网 View technologies 介绍 传送门

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值