当前位置:网站首页>About the configuration and use of thymeleaf
About the configuration and use of thymeleaf
2022-07-29 05:11:00 【jakeonil】
First
To configure a about thymeleaf I have a library here zip Compressed package , You can complete the corresponding configuration , If it's in idea If you guide the bag by yourself , Will report a mistake , And the compressed package is incomplete . Here is a website that has thymeleaf Description of https://heavy_code_industry.gitee.io/code_heavy_industry/pro001-javaweb/lecture/chapter08/verse03.html
After importing the package number , I'm going to build a new one servlet class , You can call it viewbaseservlet
The writing of the inside bag is :
package Servlet;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ViewBaseServlet extends HttpServlet {
private TemplateEngine templateEngine;
@Override
public void init() throws ServletException {
// 1. obtain ServletContext object
ServletContext servletContext = this.getServletContext();
// 2. establish Thymeleaf Parser object
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
// 3. Set parameters to the parser object
// ①HTML It's the default mode , The explicit setting is to make the code easier to understand
templateResolver.setTemplateMode(TemplateMode.HTML);
// ② Set prefix
String viewPrefix = servletContext.getInitParameter("view-prefix");
templateResolver.setPrefix(viewPrefix);
// ③ Set suffix
String viewSuffix = servletContext.getInitParameter("view-suffix");
templateResolver.setSuffix(viewSuffix);
// ④ Set cache expiration time ( millisecond )
templateResolver.setCacheTTLMs(60000L);
// ⑤ Set whether to cache
templateResolver.setCacheable(true);
// ⑥ Set the server-side encoding method
templateResolver.setCharacterEncoding("utf-8");
// 4. Create template engine object
templateEngine = new TemplateEngine();
// 5. Set the template parser for the template engine object
templateEngine.setTemplateResolver(templateResolver);
}
protected void processTemplate(String templateName, HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 1. Set the content type and character set of the response body
resp.setContentType("text/html;charset=UTF-8");
// 2. establish WebContext object
WebContext webContext = new WebContext(req, resp, getServletContext());
// 3. Process template data
templateEngine.process(templateName, webContext, resp.getWriter());
}
}Then build another servlet Class to inherit this class , Complete the request sent to the client
Be careful :
Physical view = View prefix + Logical view + View suffix
<!-- Configure the view prefix and view suffix in the context parameters -->
<context-param>
<param-name>view-prefix</param-name>
<param-value>/WEB-INF/view/</param-value>
</context-param>
<context-param>
<param-name>view-suffix</param-name>
<param-value>.html</param-value>
</context-param>The method of the parent class can be called in the inherited subclass , Corresponding to relevant html Page resources of
stay doGet() Method to jump to Thymeleaf page
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. Declare the name of the view to which the current request is going
String viewName = "target";
// 2. call ViewBaseServlet Method of parsing view template in parent class
super.processTemplate(viewName, request, response);
}边栏推荐
- DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
- Opencv learning 1 (environment configuration)
- JS daily question (11)
- What servers are needed to build mobile app
- How does excel filter out the content you want? Excel table filtering content tutorial
- Improve the readability of your regular expressions a hundred times
- js(forEach)出现return无法结束函数的解决方法
- WPS如何进行快速截屏?WPS快速截屏的方法
- Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
- Quick start JDBC
猜你喜欢

ODOO开发教程之图表

AUTOSAR from introduction to proficiency 100 lectures (78) -autosar-dem module

How to add a map to the legendary server

pytorch学习笔记

Improve the readability of your regular expressions a hundred times

MySQL regularly calls preset functions to complete data update

A little knowledge about management

【文件下载】Easyexcel快速上手

Excel怎么筛选出自己想要的内容?excel表格筛选内容教程

Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
随机推荐
Pivot table of odoo development tutorial
MySQL定时调用预置函数完成数据更新
Northeast University Data Science Foundation (matlab) - Notes
怎样监测微型的网站服务
Understand activity workflow
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
电脑无法打开excel表格怎么办?excel打不开的解决方法
Solution | get the relevant information about the current employees' highest salary in each department |
2021-10-11
How to add a map to the legendary server
Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
Solve the warning prompt of MySQL mapping file
[wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back
Legend how to configure multiple versions of wechat updates on one server
Force deduction ----- sort odd and even subscripts respectively
ODOO开发教程之透视表
【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】
1 sentence of code, get asp Net core binds multiple sources to the same class
Use annotation test in idea
js(forEach)出现return无法结束函数的解决方法