java - Thymeleaf 在一页上呈现另一页错误

标签 java html spring-boot thymeleaf

我可以将汽车列表返回到 localhost:8080/list 但不能返回 本地主机:8080/car。我的目标是在 car.html 中的汽车表单下方呈现列表。

使用 Thymeleaf 渲染列表时,我尝试了两种方案:通过映射到 /list 的 Controller 方法 getAllCars() 我能够在 上渲染列表>list.html 但是,使用映射到 /car 的相同方法,在尝试渲染到 car.html 时出现错误。

我确信我在这里遗漏了一些小东西。也许是我在尝试在 car.html 上呈现列表时使用了 Controller 方法。我在 list.html 上成功使用的 html 与我使用汽车表单将其添加到 car.html 时使用的相同。我只是注释/取消注释尝试每种场景所需的代码。

工作场景:在 CarController 中使用 getAllCars() 方法映射到 list.html

@Controller
public class CarController {

    @Autowired
    private CarService carService;

    //PRESENT VIEW car.html
    @GetMapping("/car")
    public String carForm(Model model) {
        model.addAttribute("car", new Car());
        return "car";
    }

    //GET list of cars. render to /list
    @RequestMapping("/list")
    public String getAllCars(Model model) {
        model.addAttribute("cars", carService.getAllCars());
        return "list";
    }

    //GET list of cars. Render to /car
//    @RequestMapping("/car")
//    public String getAllCars(Model model) {
//        model.addAttribute("cars", carService.getAllCars());
//        return "car";
//    }

    //POST TO DATABASE
    @PostMapping("/car")
    public void carSubmit(@ModelAttribute Car car) {
        carService.addCar(car);
    }

}

list.html --> 这工作正常

这是html代码

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Car List</h1>

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Make</th>
            <th>Model</th>
            <th>Year</th>
        </tr>
    </thead>
    <tbody>
        <!-- if true, display this       -->
        <tr th:if="${!cars}">
            <td colspan="2"> No Cars Available</td>
        </tr>
        <!--  otherwise, iterate over list and display info      -->
        <tr th:each="car : ${cars}">
            <td><span th:text="${car.id}">Id</span></td>
            <td><span th:text="${car.make}">Make</span></td>
            <td><span th:text="${car.model}">Model</span></td>
            <td><span th:text="${car.year}">Year</span></td>

    </tbody>
</table>
</body>
</html>

非工作场景:在 CarController 中使用 getAllCars() 方法映射到 car.html

为简洁起见,请参阅上面 CarController 中带注释的 getAllCars() 方法 car.html --> 这不起作用

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Car Form</h1>
<form action="#" th:action="@{/car}" th:object="${car}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>Make: <input type="text" th:field="*{make}" /></p>
    <p>Model: <input type="text" th:field="*{model}" /></p>
    <p>Year: <input type="text" th:field="*{year}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

<h1>Car List</h1>

<table>
    <thead>
    <tr>
        <th>Id</th>
        <th>Make</th>
        <th>Model</th>
        <th>Year</th>
    </tr>
    </thead>
    <tbody>
    <!-- if true, display this       -->
    <tr th:if="${!cars}">
        <td colspan="2"> No Cars Available</td>
    </tr>
    <!--  otherwise, iterate over list and display info      -->
    <tr th:each="car : ${cars}">
        <td><span th:text="${car.id}">Id</span></td>
        <td><span th:text="${car.make}">Make</span></td>
        <td><span th:text="${car.model}">Model</span></td>
        <td><span th:text="${car.year}">Year</span></td>

    </tbody>
</table>
</body>
</html>

这就是我面临的错误

2019-08-27 12:30:32.519 ERROR 22384 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "car": Exception evaluating SpringEL expression: "!cars" (template: "car" - line 33, col 9)

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "!cars" (template: "car" - line 33, col 9)

最佳答案

我想我明白了你的问题。您正在重定向到带有 Controller 的 car.html 页面

 @GetMapping("/car")
    public String carForm(Model model) {
        model.addAttribute("car", new Car());
        return "car";
    }

在同一页面中,您可以显示您的汽车列表并以表格保存。但是您没有将带有 modelcars 放入此 Controller 中。这就是您收到模板错误的原因。要解决此问题,您需要将 cars 放入之前的 Controller 中。因此修改你的 Controller

 @GetMapping("/car")
    public String carForm(Model model) {
        model.addAttribute("car", new Car());
        return "car";
    }

@GetMapping("/car")
public String carForm(Model model) {
    model.addAttribute("car", new Car());
    model.addAttribute("cars", carService.getAllCars());
    return "car";
}

现在,在发布汽车后,您必须重定向到一个页面。但你没有那样做。因此修改你的 Controller

 @PostMapping("/car")
    public void carSubmit(@ModelAttribute Car car) {
        carService.addCar(car);
    }

 @PostMapping("/car")
    public String carSubmit(@ModelAttribute Car car) {
        carService.addCar(car);
        return "redirect:/car"
    }

希望它能起作用。大声喊出来!

关于java - Thymeleaf 在一页上呈现另一页错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57680065/

相关文章:

javascript - 将应用程序部署到 Heroku,在后端 Java Spring Boot 中抛出 500 内部服务器错误,同时在本地环境中完美运行

java - Flask/Pymongo 对 Java 的 JSON 响应

java - 如何在 Play Framework 的 head 和 body 部分添加 HTML 内容?

java - for 循环中缺少 return 语句 - return int 表示商店中所有 Customer 对象的平均年龄

javascript - 使导航栏在CSS中占据整个页面高度

jquery - 如何在点击切换开关时增加值(value)?

java - MySQL(谷歌云SQL) : User Access denied only from Java

java - java安全管理器可以在安装级别默认启用吗?

javascript - 使用值选择 <select> 标签选项

spring-boot - Netty - EventLoop 队列监控