您的位置:首页 > 编程语言 > Java开发

Thymeleaf模板与Spring MVC集成

2017-12-21 17:02 302 查看
Thymeleaf 定义了 org.thymeleaf.spring4.view.ThymeleafView 和 org.thymeleaf.spring4.view.ThymeleafViewResolver(默认使用ThymeleafView 作为 View)。Thymeleaf 提供了一个SpringTemplateEngine 类,用来驱动在 Spring MVC 下使用
Thymeleaf 模板引擎,还提供了一个 TemplateResolver 来设置通用的模板引擎(包括前缀、后缀等),在 Spring MVC中集成Thymeleaf 引擎很简单。示例:

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafView;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

/**
* Created by Administrator on 2017/12/21.
*/
@Configuration
public class ConfigApplication {

@Bean
public TemplateResolver templateResolver() {
TemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setViewClass(ThymeleafView.class);
return thymeleafViewResolver;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: