博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring整合thymeleaf
阅读量:6969 次
发布时间:2019-06-27

本文共 3481 字,大约阅读时间需要 11 分钟。

hot3.png

官方文档入口:

1、首先需要引入thymeleaf的依赖(据官网文档,thymeleaf-spring3与thymeleaf-spring4用法基本一致)

org.thymeleaf
thymeleaf-spring4
3.0.11.RELEASE

2、配置thymeleaf的模板解析器、模板引擎与视图解析器

官方文档以xml作为配置方法,因为不是很方便,此处使用java方式进行配置

import javax.servlet.ServletContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.web.context.ServletContextAware;import org.thymeleaf.spring4.SpringTemplateEngine;import org.thymeleaf.spring4.view.ThymeleafViewResolver;import org.thymeleaf.templateresolver.ServletContextTemplateResolver;@ComponentScan(basePackages="com.example.demo.controller")public class ServletConfig implements ServletContextAware {    private ServletContext servletContext;    @Override    public void setServletContext(ServletContext servletContext) {        this.servletContext = servletContext;    }    /* 加载thymeleaf模板 */    @Bean    public ServletContextTemplateResolver templateResolver() {        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(this.servletContext);        resolver.setPrefix("/WEB-INF/templates/");        resolver.setSuffix(".html");        resolver.setTemplateMode(TemplateMode.HTML);        resolver.setCharacterEncoding("UTF-8");        resolver.setCacheable(true);        return resolver;    }/* 模板引擎,渲染并返回结果 */    @Bean    public SpringTemplateEngine templateEngine() {        SpringTemplateEngine templateEngine = new SpringTemplateEngine();        templateEngine.setTemplateResolver(templateResolver());        templateEngine.setEnableSpringELCompiler(true);        return templateEngine;    }    /* 视图解析器 */    @Bean    public ThymeleafViewResolver viewResolver() {        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();        viewResolver.setTemplateEngine(templateEngine());        viewResolver.setCharacterEncoding("UTF-8");        return viewResolver;    }}

在配置模板解析器的时候,我选择的是实现ServletContextAware接口以获取ServletContext,并以此为参数创建ServletContextTemplateResolver。此处应该还可以用另一种方式进行配置:

public class WebConfig implements ApplicationContextAware{    private ApplicationContext applicationContext;    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException    {        this.applicationContext = applicationContext;    }    //加载 Thymeleaf 模板    @Bean    public SpringResourceTemplateResolver templateResolver()    {        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();        templateResolver.setApplicationContext(this.applicationContext);        templateResolver.setPrefix("/WEB-INF/templates/");        templateResolver.setSuffix(".html");        return templateResolver;    }}

3、测试:

@RequestMapping("/test")    public String test(Model model) throws IOException {        List
userList = baseService.queryUsers(); model.addAttribute("userList", userList); return "test"; }
测试页面

4、此外,html页面常需要引入静态文件,为了饮用方便以及避免静态文件路径错误导致的异常,需要通过WebMvcConfigurer接口设置静态文件的根路径,避免路径错误导致的异常

@EnableWebMvcpublic class ServletConfig implements WebMvcConfigurer,ServletContextAware {    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/templates/static/");    }}

注:需要在类上加入@EnableWebMvc注解表示启用java config,否则此方法不会生效。

转载于:https://my.oschina.net/u/4108765/blog/3059604

你可能感兴趣的文章
java线程之一 单线程
查看>>
node学习笔记
查看>>
ts 使用Visual Studio2012和TFS网站管理源代码
查看>>
Agreement has been updated--Edit Phone Number最便捷解决办法(不需要安全提示问题和双重认证)...
查看>>
ImportError: No module named mysql 报错python引用mysql报错
查看>>
【luogu 1962】斐波那契数列
查看>>
团队冲刺站立会议01
查看>>
Java的类加载
查看>>
坐标系转换
查看>>
CSS3 Animation
查看>>
Jsp注册页面身份证验证
查看>>
hdu 1011(树形dp)
查看>>
学习微信小程序之css15解决父盒子高度塌陷
查看>>
Leetcode_638.Shopping Offers
查看>>
LC.35.Search Insert Position
查看>>
Spring的bean创建详解
查看>>
Python偏函数实例
查看>>
Java基础 - 变量的定义和使用
查看>>
Quartz Spring与Spring Task总结
查看>>
Centos安装桌面环境(一个命令搞定)
查看>>