【问题标题】:How to get data from URL with Spring Boot?如何使用 Spring Boot 从 URL 获取数据?
【发布时间】:2022-10-15 02:19:50
【问题描述】:

基本上,我需要从我的 URL 中获取数据。

HTML 代码如下所示:

<h4><a th:text="${h.nombre}" class="card-title" style="color: #1c1f23;text-decoration: none;" th:href="@{/hoteles/{id}(id=${h.getId()})}"></a></h4>

我得到的那种 URL 的例子:

localhost:8080/hoteles/3

现在,我需要得到那个号码并将其放在控制器上。

我当前的控制器如下所示:

@RequestMapping("/hoteles/{item}")
public @ResponseBody ModelAndView resultadoHotel(@PathVariable(value="item") String numerito,
                                                 @RequestParam Integer id) {
    List<Hotel> listaHoteles = hotelService.getAll();
    BuscadorID numero = new BuscadorID(id);
    Hotel definitivo = buscadorService.Comparar(numero,listaHoteles);
    ModelAndView model = new ModelAndView("hotelWeb");
    model.addObject("definitivo", definitivo);
    return model;
}

我只是不知道我是否做错了什么。我不明白 RequestParam 是如何工作的。

【问题讨论】:

  • localhost:8080/hotels/3?id=5 是您的 URL 的外观。 item 将是“3”作为 String 和 id 5 作为 Int。 PathVariables = URL 的必需部分。 PathParams = 之后的可选过滤器?部分并由 , 符号分隔。

标签: java spring-boot thymeleaf


【解决方案1】:

在没有值的 RequestParam 中,默认情况下,它根据文档需要一个空字符串。 尝试这个...

@RequestMapping("/hoteles/{item}")
    public @ResponseBody ModelAndView resultadoHotel(@PathVariable(value="item") String numerito,
                                                     **@RequestParam(value = "id")** Integer id) {
        List<Hotel> listaHoteles = hotelService.getAll();
        BuscadorID numero = new BuscadorID(id);
        Hotel definitivo = buscadorService.Comparar(numero,listaHoteles);
        ModelAndView model = new ModelAndView("hotelWeb");
        model.addObject("definitivo", definitivo);
        return model;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多