当前位置:网站首页>关于thymeleaf的配置与使用
关于thymeleaf的配置与使用
2022-07-29 05:06:00 【jakeonil】
首先
要去配置一个关于thymeleaf的库我这里下了一个zip压缩包,可以完成相应的配置,如果是在idea自己去导包的话,会报错,而且压缩包不全。这里推荐一个网站里面有thymeleaf的相关说明https://heavy_code_industry.gitee.io/code_heavy_industry/pro001-javaweb/lecture/chapter08/verse03.html
导入包号以后,就要去新建一个servlet类,可以命名为viewbaseservlet
里面包的写法是:
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.获取ServletContext对象
ServletContext servletContext = this.getServletContext();
// 2.创建Thymeleaf解析器对象
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
// 3.给解析器对象设置参数
// ①HTML是默认模式,明确设置是为了代码更容易理解
templateResolver.setTemplateMode(TemplateMode.HTML);
// ②设置前缀
String viewPrefix = servletContext.getInitParameter("view-prefix");
templateResolver.setPrefix(viewPrefix);
// ③设置后缀
String viewSuffix = servletContext.getInitParameter("view-suffix");
templateResolver.setSuffix(viewSuffix);
// ④设置缓存过期时间(毫秒)
templateResolver.setCacheTTLMs(60000L);
// ⑤设置是否缓存
templateResolver.setCacheable(true);
// ⑥设置服务器端编码方式
templateResolver.setCharacterEncoding("utf-8");
// 4.创建模板引擎对象
templateEngine = new TemplateEngine();
// 5.给模板引擎对象设置模板解析器
templateEngine.setTemplateResolver(templateResolver);
}
protected void processTemplate(String templateName, HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 1.设置响应体内容类型和字符集
resp.setContentType("text/html;charset=UTF-8");
// 2.创建WebContext对象
WebContext webContext = new WebContext(req, resp, getServletContext());
// 3.处理模板数据
templateEngine.process(templateName, webContext, resp.getWriter());
}
}
然后再去建一个servlet类去继承这个类,完成对客户端发来的请求
注意:
物理视图=视图前缀+逻辑视图+视图后缀
<!-- 在上下文参数中配置视图前缀和视图后缀 -->
<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>
而在继承的子类中可以调用父类的方法,对应到相关的html的页面资源上
在doGet()方法中跳转到Thymeleaf页面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.声明当前请求要前往的视图名称
String viewName = "target";
// 2.调用ViewBaseServlet父类中的解析视图模板的方法
super.processTemplate(viewName, request, response);
}
边栏推荐
- What if the office prompts that the system configuration cannot run?
- Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
- [untitled]
- Pytorch learning notes
- 电脑无法打开excel表格怎么办?excel打不开的解决方法
- 【2022新生学习】第三周要点
- How to solve the problem of configuring the progress every time Office2010 is opened?
- Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
- 向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
- MySQL定时调用预置函数完成数据更新
猜你喜欢
使用Jstack、Jconsole和jvisualvm进行死锁分析
IOS interview preparation - Online
2021-10-23
学习数据库的第一个程序
Network Security Learning - Intranet Security 1
荣耀2023内推,内推码ambubk
Improve the readability of your regular expressions a hundred times
Climbing the pit of traffic flow prediction (III): using pytorch to realize LSTM to predict traffic flow
How does word view document modification traces? How word views document modification traces
Solve the warning prompt of MySQL mapping file
随机推荐
The difference between the two ways of thread implementation - simple summary
Quick start JDBC
Unity metaverse (III), protobuf & socket realize multi person online
IOS interview preparation - other articles
def fasterrcnn_ resnet50_ FPN () instance test
C language implementation of three chess
Numpy basic learning
Introduction of JDBC preparestatement+ database connection pool
Take you to understand JS array
Simple user-defined authentication interface rules
Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address
网安学习-内网安全1
JS daily question (11)
EMI interference troubleshooting with near-field probe and current probe
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
PHP determines whether the user has logged in. If logged in, the home page will be displayed. If not, enter the login page or registration page
What if excel is stuck and not saved? The solution of Excel not saved but stuck
Climbing the pit of traffic flow prediction (II): the simplest LSTM predicts traffic flow using tensorflow2
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
IDEA中使用注解Test