0

我正在尝试匹配此模式:@RequestMapping("/hike/{id}"),示例 url 将是 localhost:8080/hike/1

所以在我的模板中我有这一行:@{/hike/{id}(id=${hike.id})}它创建了错误的 url。有谁知道如何根据 RequestMapping 调整此行以创建 url?

编辑:

这是我正在使用的控制器:

 @RequestMapping(value = "hike/{id}", method = RequestMethod.GET)
    public String hikeId(@PathVariable("id") long id, Model model){
        Hike hike = hikeService.findById(id).orElseThrow(()->new IllegalArgumentException("Invalid Hike Id"));
        model.addAttribute("hikes", hike);
        return "Welcome";
    }

编辑:

我的 href 不是 th:href,但它现在是,它仍然无法正常工作。我的模板如下所示:

<tbody>
  <tr th:each="hike : ${hikes}">
    <td th:text="${hike.name}"></td>
    <td><a th:href="@{/hike/${hike.id}}">SELECT</a></td>
  </tr>
</tbody>
4

2 回答 2

1

试试这个:

If Controller like this: 

@RequestMapping(value="hike/{id}", method = RequestMethod.GET)
public String findById(@PathVariable Long id) {
             System.out.println("ID"+id);
        return "ID found"+id;
}



Then template willbe:

 <a th:href="@{'/hike/' + ${hike.id}}">Find by Id</a>

或者

**Controller:**

    @GetMapping(value="/hike/find-by-id")
    public String restore(Model model, @RequestParam Long id){
            System.out.println("ID"+id);
        return "ID found"+id;
    }


**Template:**

  <a type="button" th:href="@{/hike/find-by-id(id=${hike.id})}">Find by Id</a>
于 2020-11-01T04:18:51.070 回答
1

您可以使用生成 URL

<a th:href="@{/hike/{id}/(id=${hike.id})}">SELECT</a>

或者

<a th:href="@{|/hike/${hike.id}|}">SELECT</a>
于 2020-11-01T18:58:52.197 回答