此教程使用的Thymeleaf版本为 3.1.2.RELEASE,JDK最低要求为1.8
下载地址:https://pan.baidu.com/s/1HJf2AnPu1nwaxmOxzxUDJA?pwd=au9c
1、解压以下压缩包,
2、复制dist目录下的thymeleaf-3.1.2.RELEASE.jar到项目中的lib目录下
3、复制lib目录下所有的jar文件到项目中的lib目录下
1、创建包:在项目包下创建com.thymeleaf包,此包用于定义配置thymeleaf的类
2、在包下创建ThymeleafConfig类,此类用于创建thymeleaf模板引擎,解析器
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
/**
* 定义thymeleaf模板类,在全局对象中注册thymeleaf模板引擎
* @author 创蓝IT教育
*
*/
public class TemplateConfig {
private static final String TEMPLATE_ENGINE_ATTR = "com.e-learn.thymeleaf3.TemplateEngineInstance";
//全局注册thymeleaf模板引擎
public static void storeTemplateEngine(ServletContext context) {
TemplateEngine engine = templateEngine(context);
context.setAttribute(TEMPLATE_ENGINE_ATTR, engine);
}
/**
* 创建模板引擎
*/
public static TemplateEngine getTemplateEngine(ServletContext context) {
return (TemplateEngine) context.getAttribute(TEMPLATE_ENGINE_ATTR);
}
/**
* 创建thymeleaf模板引擎
* @param servletContext
* @return
*/
private static TemplateEngine templateEngine(ServletContext servletContext) {
TemplateEngine engine = new TemplateEngine();
engine.setTemplateResolver(templateResolver(servletContext));
return engine;
}
/**
* 创建thymeleaf解析对象
* @param servletContext
* @return
*/
private static ITemplateResolver templateResolver(ServletContext servletContext) {
//设置解析参数,常用参数有:模板页面路径前缀,路径的后缀,解析编码,模板文件类型
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(servletContext);
resolver.setPrefix("/WEB-INF/");//模板页面路径前缀
resolver.setSuffix(".html"); //模板页面路径后缀
resolver.setCharacterEncoding("UTF-8"); //解析的编码
resolver.setTemplateMode(TemplateMode.HTML); //模板文件的类型
return resolver;
}
}
3、在项目包下创建listener包,在其中创建全局监听器AppLinstener,实现ServletContextListener,在contextInitialized方法中调用TemplateConfig的templateEngine方法注册thymeleaf模板引擎
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.thymeleaf.demo.utitl.TemplateConfig;
@WebListener
public class AppListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
TemplateConfig.storeTemplateEngine(sce.getServletContext());
}
}
1、在WEB-INF下创建index.html页面,在body中添加以下内容
链接
2、在项目包下创建servlet包,在包中创建TestServlet类,设置访问路径为"/test"
3、在TestServlet类的doPost方法中使用引擎渲染页面
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import com.thymeleaf.demo.utitl.TemplateConfig;
@WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取模板引擎
TemplateEngine engine = TemplateConfig.getTemplateEngine(getServletContext());
//当前请求环境
WebContext context = new WebContext(request, response, getServletContext());
response.setCharacterEncoding("utf-8");
//使用模板渲染页面并输出到浏览器
engine.process("index", context, response.getWriter());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
1、启动服务
2、在浏览器中输入test访问路径,运行效果如下
文章来源于哔站《二、thymeleaf与javaweb的集成》
更多学习视频和专栏文章请到哔站个人空间: 布道师学院的个人空间-布道师学院个人主页-哔哩哔哩视频
更多资源和项目下载请到:”开源吧(找实战项目和毕设项目的好网站)“ :开源吧