在本教程中,我将向您展示如何将百里叶与雅加达 EE Servlet 集成,以替换在雅加达 EE Servlet 应用程序中使用 JSP。
首先,我将创建一个新的雅加达 EE Servlet Maven 项目作为示例:
百里叶依赖性如下:
1
2
3
4
5
|
|
我将创建一个新的 Servlet 来处理来自用户的请求:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.huongdanjava.jakartaee.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/")
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
}
|
在上面的索引服务器的 doGet() 方法中,我们将使用百里叶的模板引擎对象来处理请求的响应。
为此,首先,我们需要有模板引擎对象。但是要具有模板引擎对象,我们需要 ITemplateResolver 接口的对象来配置模板文件的位置。
ITemplateResolver 接口有许多实现,可以通过不同的方式将位置配置为模板文件:
我将使用 Web 应用程序模板解决方案作为本教程的示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private WebApplicationTemplateResolver templateResolver(IWebApplication application) {
WebApplicationTemplateResolver templateResolver = new WebApplicationTemplateResolver(application);
// HTML is the default mode, but we will set it anyway for better understanding of code
templateResolver.setTemplateMode(TemplateMode.HTML);
// This will convert "home" to "/WEB-INF/templates/home.html"
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
// Set template cache TTL to 1 hour. If not set, entries would live in cache until expelled by LRU
templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
// Cache is set to true by default. Set to false if you want templates to be automatically updated when modified.
templateResolver.setCacheable(true);
return templateResolver;
}
|
现在,您可以实例化模板引擎对象:
1
2
3
4
5
6
7
8
|
private ITemplateEngine templateEngine(IWebApplication application) {
TemplateEngine templateEngine = new TemplateEngine();
WebApplicationTemplateResolver templateResolver = templateResolver(application);
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
|
您需要在应用程序启动并运行后立即将百合饼的模板引擎对象注册到 ServletContext 中。我们将使用 Servlet 上下文侦听器来执行此操作,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package com.huongdanjava.jakartaee.servlet;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.WebApplicationTemplateResolver;
import org.thymeleaf.web.IWebApplication;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;
@WebListener
public class ThymeleafConfiguration implements ServletContextListener {
public static final String TEMPLATE_ENGINE_ATTR = "com.huongdanjava.thymeleaf.TemplateEngineInstance";
private ITemplateEngine templateEngine;
private JakartaServletWebApplication application;
@Override
public void contextInitialized(ServletContextEvent sce) {
this.application = JakartaServletWebApplication.buildApplication(sce.getServletContext());
this.templateEngine = templateEngine(this.application);
sce.getServletContext().setAttribute(TEMPLATE_ENGINE_ATTR, templateEngine);
}
private ITemplateEngine templateEngine(IWebApplication application) {
TemplateEngine templateEngine = new TemplateEngine();
WebApplicationTemplateResolver templateResolver = templateResolver(application);
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
private WebApplicationTemplateResolver templateResolver(IWebApplication application) {
WebApplicationTemplateResolver templateResolver = new WebApplicationTemplateResolver(application);
// HTML is the default mode, but we will set it anyway for better understanding of code
templateResolver.setTemplateMode(TemplateMode.HTML);
// This will convert "home" to "/WEB-INF/templates/home.html"
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
// Set template cache TTL to 1 hour. If not set, entries would live in cache until expelled by LRU
templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
// Cache is set to true by default. Set to false if you want templates to be automatically updated when modified.
templateResolver.setCacheable(true);
return templateResolver;
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// NOP
}
}
|
如您所见,我们将此模板引擎对象保存到 ServletContext 中,以便每次请求进入应用程序时,我们都会检索它并处理该请求的响应。
现在,我将修改上面的 servlet 以获取和使用模板引擎,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.huongdanjava.jakartaee.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.web.IWebExchange;
import org.thymeleaf.web.servlet.JakartaServletWebApplication;
@WebServlet("/")
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
TemplateEngine templateEngine = (TemplateEngine) getServletContext().getAttribute(
ThymeleafConfiguration.TEMPLATE_ENGINE_ATTR);
IWebExchange webExchange = JakartaServletWebApplication.buildApplication(getServletContext())
.buildExchange(req, resp);
WebContext context = new WebContext(webExchange);
context.setVariable("name", "Huong Dan Java");
templateEngine.process("home", context, resp.getWriter());
}
}
|
模板引擎对象的进程() 方法将处理应用程序的响应。此方法的参数包括模板名称、从当前 servlet 的请求和响应构建的 WebContext 对象、从当前 servlet 的响应中将响应写入用户的 Writer 对象。
主.html文件在目录/主目录/网络应用目录下,目录下文件的内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
xmlns:th="http://www.thymeleaf.org">
Hello world
from !
|
删除 src/主/webapp/WEB-INF/ 目录中的 web.xml文件(因为我们使用的是@WebServlet注释)和 src/主/webapp/ 目录中的索引.jsp文件,然后运行应用程序,您将看到以下结果:
- "1.0" encoding="UTF-8"?>
-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>LearnJSPServletgroupId>
- <artifactId>jakartaee-servlet-thymeleafartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <packaging>warpackaging>
-
- <name>jjakartaee-servlet-thymeleaf Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.7maven.compiler.source>
- <maven.compiler.target>1.7maven.compiler.target>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>jakarta.servletgroupId>
- <artifactId>jakarta.servlet-apiartifactId>
- <version>6.0.0version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleafartifactId>
- <version>3.1.0.M1version>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <finalName>jakartaee8finalName>
- <pluginManagement>
- <plugins>
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- plugins>
- pluginManagement>
- build>
- project>
需要Tomcat 10运行。