You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中@Controller与Thymeleaf视图正常运行但代码标红求助

问题:Spring Boot中Thymeleaf代码在IDE中标红,但运行正常

我的Spring Boot应用启动后,@Controller可正常与Thymeleaf视图配合工作,但编写HTML文件时,Thymeleaf相关代码被标红,导致难以排查HTML错误。

问题截图

Thymeleaf代码标红截图1
Thymeleaf代码标红截图2

控制器代码

package com.nikita.al_fp.controllers;

import com.nikita.al_fp.entity.Book;
import com.nikita.al_fp.entity.Person;
import com.nikita.al_fp.service.BookService;
import com.nikita.al_fp.service.PersonService;
import com.nikita.al_fp.util.BookValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

@Controller
@RequestMapping("/book")
public class BookController {

    private final BookValidator bookValidator;
    private final PersonService personService;
    private final BookService bookService;
    public static final String BOOK_REDIRECT_PAGE = "redirect:/book";

    @Autowired
    public BookController(BookValidator bookValidator, PersonService personService, BookService bookService) {
        this.bookValidator = bookValidator;
        this.personService = personService;
        this.bookService = bookService;
        personService.startProgramProcessPerson();
        bookService.startProgramProcessBook();
    }

    @GetMapping
    public String selectBook(Model model) {
        model.addAttribute("books", bookService.findAll());
        return "/book/select_book";
    }
}

HTML视图代码

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

<div th:each="book : ${books}">
    <a th:href="@{/book/{id}(id=${book.getId()})}"
       th:text="${book.getName() + ', ' + book.getAuthor() + ', ' + book.getYear()}">book</a>
    <br/>
</div>

<br/>
<hr/>

<a href="/book/new">Add book</a>
<br>
<a href="/people">Show User List</a>

</body>
</html>

Maven配置

<?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>3.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nikita</groupId>
    <artifactId>al_fp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>al_fp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我已尝试在网上寻找解决方案但未成功,特向大家求助。

内容的提问来源于stack exchange,提问作者Никита Мозолевский

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 19:15:31