春季靴子没有从百里香叶返回值

问题描述 投票:0回答:1

我是Spring boot Development的新手,我试图找出为什么我的程序没有将值返回给html。我尝试了很多没有效果的例子。我很感激你的帮助。

    @GetMapping("/produto/{description}")
public String getLike(Model model,@PathVariable("description") String description){
    List<Produto> produtos =  (List<Produto>) productService.findLike(description);
    model.addAttribute("produtos",produtos);
    System.out.println(produtos);
    return "redirect:/static/produtos.html";
}

然后尝试重定向到这个..

<!DOCTYPE HTML>
 <html xmlns:th="http://www.thymeleaf.org">
 <head>
 <title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<tr th:each="produtos : ${produtos}">
<td><span th:text="${produtos.id}"></span></td>
<td><span th:text="${produtos.name}"></span></td>
<td><span th:text="${produtos.description}"></span></td>
<td><span th:text="${produtos.price}"></span></td>
</tr>

</html>

当我不是返回模型而是通过json客户端返回一个列表时它会工作并返回所有内容。但是,当它是一个模型。它不起作用并返回此...

 redirect:/static/produtos.html

当我使用得到这个。

http://localhost:8047/produto/lenco

但是应该在html中返回这个

[
{
    "id": "223334455",
    "name": "lonco",
    "description": "lenco",
    "price": 83223
}
]
java spring web
1个回答
2
投票

您无法通过重定向执行此操作。在重定向上,您的模型属性将丢失。

你有几个选择。

  1. 只需返回/static/produtos.html。除非您重定向到另一个控制器,否则重定向没有意义。
  2. 在您的请求方法中使用RedirectAttributespublic String getLike(Model model, @PathVariable("description") String description, RedirectAttributes redirectAttributes){ List<Produto> produtos = (List<Produto>)productService.findLike(description); redirectAttributes.addFlashAttribute("produtos",produtos); return "redirect:/static/produtos.html"; }
© www.soinside.com 2019 - 2024. All rights reserved.