当 id 传递给视图时,无法显示基于数据库 id 的结果

Cannot display a result based on a database id when id is passed to view

<table class="table">

<tr>
    <th>Name</th>

</tr>

<tr th:each="recipe : ${recipes}">
    <td th:text="${recipe.Name}"></td>
    <td>
            <span th:each="recipe,iterStat : ${recipes}">

            </span>
    </td>
    <td>
        <a th:href="@{/recipe/food/{id}(id=${recipe.id})}">view</a>
    </td>
</tr>

我正在尝试点击上面的这个 link,使用这个控制器

    @RequestMapping(value="recipe/food/{id}", method = RequestMethod.POST)
public String viewRecipe(@PathVariable int id, Model model){
    model.addAttribute("name", recipeDao.findOne(id));
    model.addAttribute("recipeText", recipeDao.findOne(id));
    return "Recipes/food" ;
}

根据此处的 id 显示单个项目

<table class="table">

<tr>
    <th>Name</th>

</tr>
<form method ="post" th:action="recipe/food/{id}" th:object=${recipe}">
<tr th:each="recipe : ${recipes}">

    <td th:text="${recipe.name}">text</td>
    <td th:text="${recipe.recipeText}">text</td>
</tr>

<span th:errors="*{recipeText}" class="error"></span>
</form>

但是我只收到 404,但是 url 信息是正确的

当您点击 link 时,它会执行 GET 请求,因此请尝试将 Controller 中的方法更改为 method = RequestMethod.GET 或简单地删除 method = RequestMethod.POST.

更新:

第三个代码片段中的食物模板部分正在遍历名为 recipes 的列表,但 Controller 方法并未将列表作为属性添加到模型中,仅 "name" 和 "recipeText"。您想要的可能是显示您单击 link 的食谱的模板?类似于 <h4 th:text="${name}"></h4> <span th:text="${recipeText}" />