gpt4 book ai didi

java - 如何使用 Thymeleaf 制作一个简单的 Java 应用程序(不使用 Spring)

转载 作者:行者123 更新时间:2023-12-01 18:27:38 25 4
gpt4 key购买 nike

我正在关注 its website 上的官方 Thymeleaf 教程我目前在 Executing the template engine 部分.

根据我的理解,我应该已经能够运行到目前为止编码的应用程序,但我绝对不知道如何运行它。没有main(String[] args)完全运行的方法。

我尝试搜索其他教程,但它们都使用 Spring,这不是我现在正在寻找的。

任何人都知道我应该在哪里插入 main(String[] args)运行此 Thymeleaf 应用程序并查看我的 HTML 模板的方法?我不明白入口点在哪里或应该在哪里。

如果这个问题听起来很愚蠢,我提前表示歉意,并感谢您以后的回复。

<小时/>

编辑:

到目前为止,在遵循教程时我编写了 3 个 Java 文件:

所以我想到写一个像这样的主要方法: Main class containing main(String[] args) method

但我不知道如何正确实例化 servletContext,而且我什至不确定完成此操作后是否一切都会正常。

最佳答案

借用this comment on GitHub ,以下是如何在简单的应用程序中使用 Thymeleaf。

首先,添加 Thymeleaf 库(依赖项):

  • 如果您使用 Maven,请将其添加到您的 pom.xml 文件中:
    <dependencies>
    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.15.RELEASE</version>
    </dependency>
    <!-- Add this if you don't like seeing messages in stdout from SLF4J -->
    <!--
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-nop</artifactId>
    <version>1.7.36</version>
    </dependency>
    -->
    </dependencies>
  • 如果您使用 Gradle,请将其添加到您的 build.gradle[.kts] 文件中:
    dependencies {
    implementation("org.thymeleaf:thymeleaf:3.0.15.RELEASE")
    // Add this if you don't like seeing messages in stdout from SLF4J
    // implementation("org.slf4j:slf4j-nop:1.7.36")
    }

将模板 HTML 文件放入 classpathtemplates/ 子目录中(例如,在src/main/resources/templates/中)。

src/main/resources/templates/index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Sample Thymeleaf template</title>
</head>
<body>

<p th:text="|Hello, ${name}!|">Hello, user!</p>
<p th:text="|Today is ${date}.|">Today is ...</p>

</body>
</html>

如果您使用 Java 语言,则程序代码如下:

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import java.time.LocalDateTime;

public class Main {

public static void main(String[] args) {
// From Java 10, you can use var instead of declaring the type explicitly
var resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");

var context = new Context();
context.setVariable("name", "Lind");
context.setVariable("date", LocalDateTime.now().toString());

var templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);

var result = templateEngine.process("index", context);
System.out.println(result);
}
}

如果您使用 Kotlin language,这里是程序代码:

import org.thymeleaf.TemplateEngine
import org.thymeleaf.context.Context
import org.thymeleaf.templatemode.TemplateMode
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
import java.time.LocalDateTime

fun main() {
val resolver = ClassLoaderTemplateResolver().apply {
templateMode = TemplateMode.HTML
characterEncoding = "UTF-8"
prefix = "/templates/"
suffix = ".html"
}
val context = Context().apply {
setVariable("name", "Lind")
setVariable("date", LocalDateTime.now().toString())
}
val templateEngine = TemplateEngine().apply {
setTemplateResolver(resolver)
}
val result = templateEngine.process("index", context)
println(result)
}

关于java - 如何使用 Thymeleaf 制作一个简单的 Java 应用程序(不使用 Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60208627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com