首页 > 解决方案 > 错误:无法解析为表达式,th:action

问题描述

我想用 Thymeleaf 和 Spring 做一个 POST 请求。我的目标是从 HTML 接收选定的信息。但最初我只想在我从 th:action -“cidashboard/table”按下以下链接的提交按钮时重定向我。

我的控制器:

   @GetMapping("cidashboard/filter/data")
    public String allDataForFilter(Model model) {
        model.addAttribute("projectsVariants", projectVariantService.findAllProjectsVariants());
        model.addAttribute("builds", buildService.findAllBuildFromDB());
        model.addAttribute("misraMessages", misraMessagesService.findAllMisraMessagesFromDb());

        return "test2";
    }

    @PostMapping("cidashboard/table")
    public String createTable() {
        return "test1";
    }

我的html页面:

<form th:action="@/cidashboard/table" method="post">
    <select class="form-control">
        <option  th:each = "projectVariant : ${projectsVariants}" th:selected="${projectVariant.getProjectVariantId()}"  th:text="${projectVariant.getProjectVariantName()}"></option>
    </select>
    <input type="submit" value="submit"/>
</form>

我收到了这个错误:

Could not parse as expression: "@/cidashboard/table" 

标签: springthymeleaf

解决方案


由于文档,您必须将relative路径包装在brackets.

<form th:action="@{/cidashboard/table}" method="post">

解析为

<form action="/cidashboard/table" method="post">

推荐阅读