从Spring Boot MVC(Java)向Thymeleaf(HTML)发送数据,但没有显示出来

前端开发 2026-07-11

我想为一些Java代码搭建前端界面,我已经添加了一个Spring控制器类。唯一真正重要的代码是viewHomePage方法。Employee类和allemplist属性,两者都被注释掉,展示了最终对表格的期望。

package money.pension1;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

//import java.util.List;

@Controller
public class StackController {
 /*
 static class Employee {
    private static long count =0;

    private long id;
    private String name;
    private String email;
    Employee(String name, String email){
        id = ++count;
        this.name =name;
        this.email = email;
    }
    Employee(String name) {
        this(name, name + "@business.com");
    }

    @Override
    public String toString(){
        return String.format("%d %s", id, name);
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

  */
    @GetMapping("/")
    public String viewHomePage(Model model) {
        model.addAttribute("name", "Mega Corp");
//        model.addAttribute("allemplist", List.of(new Employee("Fred"), new Employee("Bob"),
//                new Employee("Bill"), new Employee("Mary")));
        return "index.html";
    }
}

这似乎可行,我能够实现从网页到Java代码的通信(包括数据),并且发送的响应看起来也正确,但我无法让HTML显示结果。

Spring调试信息

2026-03-15T18:50:01.388Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2026-03-15T18:50:01.397Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to money.pension1.MainController#viewHomePage(Model)
2026-03-15T18:50:01.422Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7]
2026-03-15T18:50:01.423Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : View name [index.html], model {name=Mega Corp, allemplist=[1 Fred, 2 Bob, 3 Bill, 4 Mary]}
2026-03-15T18:50:01.425Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to [index.html]
2026-03-15T18:50:01.428Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/index.html", parameters={}
2026-03-15T18:50:01.432Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2026-03-15T18:50:01.442Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 200
2026-03-15T18:50:01.445Z DEBUG 195840 --- [pension1] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

我尝试了在Thymeleaf中读取数据的不同方式,但都不起作用

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!-- <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.thymeleaf.org "> -->
<head>
    <meta charset=UTF-8">
    <title>Title</title>
</head>
<body>
<div class="container my-2" align="center">
    <p><span th:text="${name}"></span></p>

    <h3>Employee List for data-th-test="${name}" th:value="${name}" [(${name})]"</h3>

    <!--    <a th:href="@{/addnew}" class="btn btn-primary btn-sm mb-3" >Add Employee</a> -->
    <a href="/addnew">Add Employee</a>
        <table style="width:80%" border="1"
               class = "table table-striped table-responsive-md">
            <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Id</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="employee:${allemplist}">
                <td th:text="${employee.name}"></td>
                <td th:text="${employee.email}"></td>
                <td th:text="${employee.id}"></td>
                <!--
                <td> <a th:href="@{/showFormForUpdate/{id}(id=${employee.id})}"
                        class="btn btn-primary">Update</a>
                    <a th:href="@{/deleteEmployee/{id}(id=${employee.id})}"
                       class="btn btn-danger">Delete</a>
                </td>
                -->
                <td><a href="/update/{id}(id=${employee.id})}">Update</a></td>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

span没有输出任何内容,h3也没有插入任何数据

使用th:text="${name}" th:value="${name}" [(${name})] 的员工列表

我已经尝试 data-th-test="${name}",但没有成功。

解决方案

你大概应该在控制器中使用 return "index";

如果span对你不起作用,你可以试试blocks:

<th:block th:text="${name}"></th:block>

你也可以在h3中使用它。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章