标签: spring-thymeleaf

如何在同一个 Spring Boot 项目中同时使用 thymeleaf 和 jsp

我可以访问 templates 文件夹下的文件,但无法访问 jsp 文件。如果我删除 thymeleaf 依赖项,我就可以访问 jsp 文件,但我想使用 spring boot 访问 thymeleaf html 文件和 jsp 文件。

以下是我在 application.properties 和 pom.xml 中的配置

Spring 视图解析器设置

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
Run Code Online (Sandbox Code Playgroud)
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

下面是错误,当我尝试访问 jsp 文件时

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "todo-form", template might not exist or might not be accessible by any of the configured Template Resolvers spring
Run Code Online (Sandbox Code Playgroud)

我的视图文件夹结构

在此输入图像描述

java jsp thymeleaf spring-boot spring-thymeleaf

7
推荐指数
1
解决办法
3076
查看次数

使用地图在 thymeleaf 中设置值

I 从地图生成复选框列表。现在如何设置键的值(假/真),现在我可以在 UserConfig 中下载它,以便我可以在项目的其余部分使用该值。

我的看法:

<body>
<main>
    <form th:action="@{/file/uploadFile}" enctype="multipart/form-data" method="POST"/>
    <fieldset>
        <legend>Generate Report</legend>
            <label> Upload File
                <input name="file" type="file" required/>
                <input type="submit" value="Upload"/>
            </label>
            <label th:each="item : ${userConfig.isEnableMap}">
                <input type="checkbox" id="" th:text="${item.key}" th:value="${item.value}"/>
            </label>
        </form>
    </fieldset>
</main>
</body>
Run Code Online (Sandbox Code Playgroud)

我的类 UserConfig :

    @Component
public class UserConfig {

    private Map<String, Boolean> isEnableMap = new HashMap<>();

    public UserConfig() {
        isEnableMap.put(EnableProcess.MONTHLY_TIME_UPDATE.getName(), false);
        isEnableMap.put(EnableProcess.SUM.getName(), false);
        isEnableMap.put(EnableProcess.HIDE_COLUMNS.getName(), true);
    }

    public UserConfig(Map<String, Boolean> isEnableMap) {
        this.isEnableMap = isEnableMap;
    }

    public Map<String, Boolean> getIsEnableMap() {
        return isEnableMap; …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-thymeleaf

5
推荐指数
1
解决办法
879
查看次数

Netty 和 Spring Boot 3 中 getRequestURI 为 null

在 Thymeleaf < 3.1 中,我使用下面的表达式来获取请求 URI。

th:classappend="${#arrays.contains(urls, #httpServletRequest.getRequestURI()) ? 'active' : ''}"
Run Code Online (Sandbox Code Playgroud)

它一直有效,直到最近我升级到了 Spring Boot 3.0,它拉动了 Thymeleaf 3.1。我收到此异常:

[THYMELEAF][parallel-2] Exception processing template "index": Exception evaluating SpringEL expression: "#arrays.contains(urls, #servletServerHttpRequest.getRequestURI()) ? 'active' : ''" (template: "fragments/header" - line 185, col 6)

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getRequestURI() on null context object
Run Code Online (Sandbox Code Playgroud)

既然我在 Spring Boot 3.0 中使用 Netty 而不是 Tomcat,那么现在有什么替代方案吗?我无法从这里弄清楚这一点。

作为解决方法,现在为了解决这个问题,我正在使用:

@GetMapping ("/")
String homePage(Model model) {
    model.addAttribute("pagename", "home");
    return "index";
}
Run Code Online (Sandbox Code Playgroud)

th:classappend="${pagename == …
Run Code Online (Sandbox Code Playgroud)

spring thymeleaf spring-boot spring-thymeleaf

5
推荐指数
1
解决办法
2393
查看次数

Spring Boot ThymeLeaf 不加载 HTML

我有一个 Java 和 Maven 项目,其中有以下控制器(src/main/java/com/example/romannumerals/controller/RomanNumeralController):

@RestController
public class RomanNumeralController {
    @GetMapping("/roman-numerals/{number}")
    public String RomanNumeral(@PathVariable int number){
        return RomanNumeralsService.numberToNumeral(number);
    }

    @GetMapping("/roman-numerals")
    public String RomanNumeral() {
        return "roman-numeral";
    }
}
Run Code Online (Sandbox Code Playgroud)

我的一些 HTML (src/main/resources/templates/roman-numeral.html) 看起来像这样:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>From Numbers to Roman Numerals</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Roman Numerals
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我希望当我在控制器上调用没有参数的方法时显示此 HTML。相反,我将字符串“罗马数字”打印到屏幕上。难道我做错了什么?我认为 Thymeleaf 会识别出我想要显示具有该名称的 HTML 页面,而不是显示的字符串。

java maven spring-boot spring-thymeleaf

2
推荐指数
1
解决办法
1679
查看次数

已弃用的未包装片段表达式

您好,当使用 thymeleaf 3.1 和 spring6 单击注册按钮时,我收到以下错误,有人可以帮助我解决如何在新版本中编写给定语法的问题吗?

基础::布局(~{::节})

已弃用的未包装片段表达式“base::layout(~{::section})”在模板register.html第2行第53列中找到。请改用片段表达式的完整语法(“~{base::layout(~ {::部分})}”)。旧的、未包装的片段表达式语法将在 Thymeleaf 的未来版本中删除。

下面是我的 register.html 文件

<!DOCTYPE html>
<html  lang="en" xmlns:th="http://www.thymeleaf.org" 
       th:replace="base::layout(~{::section})">
<head>
    <meta charset="ISO-8859-1">
    <title>Insert title</title>
</head>
<body>
    <section>
        <div class="container p-3" >
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header text-center fs-4">Register Page</div>
                        <th:block th:if="${session.msg}">
                            <p class="text-center fs-3">[[${session.msg}]]</p>
                            <th:block th:text="${#session.removeAttribute('msg')}" </th:block>
                            </th:block>
                        <div class="card-body">
                            <form action="createUser" method="post">
                                <div class="mb-3">
                                    <label>Enter First Name</lable>
                                        <input type="text" name="firstname" class="form-control">
                                </div>
                                <div class="mb-3">
                                    <label>Enter Middle Name</lable>
                                        <input type="text" name="middlename" class="form-control">
                                </div>
                                <div class="mb-3"> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-thymeleaf

2
推荐指数
1
解决办法
6907
查看次数

标签 统计

spring-thymeleaf ×5

java ×4

spring-boot ×4

spring ×2

thymeleaf ×2

jsp ×1

maven ×1