如何在同一HTML文件中获取模型的两个属性

发布于 2025-02-13 04:33:45 字数 715 浏览 0 评论 0原文

方法中的代码

@RequestMapping(value = "/cars", params = "request")
public String showSomeAmountCars(HttpServletRequest request, Model model) {
    String count = request.getParameter("count");
    int parsedCount = Integer.parseInt(count);
    model.addAttribute("someCars", carService.getCars(parsedCount));

    return "cars";
}
  • showeomeamountcars 我在URL链接中使用参数获得了一些汽车。
  • 在方法Showallcars中,我获得了整个汽车列表。

预期行为

  • 如果我请求/cars,那么如果我请求/cars?count = 2,我从HTML文件中获取汽车列表
  • ,然后我获得两个列表来自同一HTML文件

问题

的汽车如何在HTML文件中获取两个添加的属性并确保它们不会冲突?

Code

@RequestMapping(value = "/cars", params = "request")
public String showSomeAmountCars(HttpServletRequest request, Model model) {
    String count = request.getParameter("count");
    int parsedCount = Integer.parseInt(count);
    model.addAttribute("someCars", carService.getCars(parsedCount));

    return "cars";
}
  • In the method showSomeAmountCars I get some number of cars using parameter in URL link.
  • In the method showAllCars I get the whole list of cars.

Expected behavior

  • if I request /cars, then I get the list of cars from the HTML file
  • if I request /cars?count = 2, then I get the list of two cars from the same HTML file

Question

How can I get the two added attributes in the HTML file and make sure they don't conflict?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2025-02-20 04:33:45

您可以使用这样的可选请求参数:

@GetMapping("/cars")
public String showSomeAmountCars(@RequestParam(name = "cars", required = false) Integer count, Model model) {


   if( count == null ) {
     model.addAttribute("cars", carService.getCars());
   } else {
     model.addAttribute("cars", carService.getCars(count));
   }
    return "cars";
}

You can have an optional request parameter like this:

@GetMapping("/cars")
public String showSomeAmountCars(@RequestParam(name = "cars", required = false) Integer count, Model model) {


   if( count == null ) {
     model.addAttribute("cars", carService.getCars());
   } else {
     model.addAttribute("cars", carService.getCars(count));
   }
    return "cars";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文