当前位置: 首页 > 知识库问答 >
问题:

Thymeleaf在Spring引导中不工作,也不显示在控制台中

阎咏思
2023-03-14

在freemarker上多次尝试工作后,我切换到thymeleaf,也面临同样的问题。当返回html文件时,它只是一个字符串值,而不是模板文件夹中的html文件。

当我在他们的控制台上观看youtube thymeleaf视频时,我不知道为什么它甚至不能在控制台中启动。

package com.example.demo;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class controller {

    @GetMapping
    public String getUser(Model model) {
        model.addAttribute("something", "welcome to the club");
        return "user";
    }
}


这是main/resources/templates中的html文件

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Code</title>
</head>
<body>
<h1 th:text="${something}"></h1>
</body>
</html>

POM. xml

<?xml version="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

共有1个答案

窦涵忍
2023-03-14

现在解决了,我将@RestController替换为@Controller,因为@RestController添加了@ResponseBody并阻止获取视图。

 类似资料:
  • 我正在使用Spring引导2.0.1。释放,使用Spring引导启动器胸腺。我有两个版本的个人电脑和手机页面。 下面是我的项目结构的一个简单版本 我想让该网站自动检测PC浏览器和手机浏览器,这样它就可以根据浏览器的类型将相同的请求URL映射到不同的页面 html文件非常简单,如下所示。 这是我的控制器的代码。 为了检测设备,我编写了以下配置类 然后当我试图运行应用程序时。我总是得到下面的错误。它似

  • 嗨,我用spring初始化器创建了一个简单的Spring Boot应用程序。我在主应用程序类的同一文件夹中添加了一个控制器。 这是给我以下错误的网址http://localhost:8080/welcome 此应用程序没有针对/错误的显式映射,因此您将其视为回退。 Sat Dec 19 12:51:44 IST 2020出现意外错误(类型=未找到,状态=404)。 如果我使用@restContro

  • 我在Hibernate4.0.1中遇到了本地查询问题。我有查询工作在数据库控制台,但不是在应用程序。我有: 名为“case”的表有两列指向同一个表--名为“cost”的表,其列为“value”。我的查询如下所示: 有人提到,'value'不是列的专有名称。这是真的,事实上这个列不是名为'value',而是有点不同。我的严格合同禁止发布任何关于代码的信息。我必须尽可能地释义它,所以我在fly中重命名

  • 我在SpringBoot api上工作,并使用具有以下属性设置的H2数据库。 当我想使用浏览器通过'http://localhost:8082/h2-console'查看H2数据库控制台时,浏览器中打开了一个带有连接和测试连接按钮的屏幕。当我单击Test Connection时,它返回成功,但当单击Connect按钮时,出现错误,即localhost拒绝连接。

  • 我想在ArrayList中添加一个名称,但是当我调用put方法时,console.log显示了一个get方法。 当我调用url时 但它应该是put方法,而不是get方法,这有什么错?