当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
虚拟偶像的歌声原来是这样生成的!
SparkSql批量插入或更新,保存数据到Mysql中
What if the computer cannot open excel? The solution of Excel not opening
自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量
Google gtest事件机制
[untitled]
Improve the readability of your regular expressions a hundred times
Jackson parsing JSON detailed tutorial
Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
The representation of time series analysis: is the era of learning coming?
随机推荐
How to open IE browser by running win command
[sudden] solve remote: support for password authentication was removed on August 13, 2021. please use a perso
Understand activity workflow
输入的查询SQL语句,是如何执行的?
传奇服务端如何添加地图
Glory 2023 push, push code ambubk
Introduction of JDBC preparestatement+ database connection pool
Traffic flow prediction pit climbing record (I): traffic flow data set, original data
时间序列分析的表示学习时代来了?
Live in small private enterprises
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
Mysql语句中的函数
A little knowledge about management
What if the computer cannot open excel? The solution of Excel not opening
How to monitor micro web services
How to add a map to the legendary server
TMUX essays
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
关于thymeleaf的配置与使用
P2181 diagonal