【问题标题】:Getting the selected values from a checkbox list to the controller with Spring Boot使用 Spring Boot 从复选框列表中获取选定的值到控制器
【发布时间】:2017-03-22 07:41:28
【问题描述】:

我正在使用带有 Thymeleaf 作为查看器的 Spring Boot,并且我想从表中删除所有选定的项目。因此,我需要向控制器传递一个包含所选复选框值的列表。

这是我的控制器方法:

@PostMapping("/admin/rates/prices/delete")
public String delete(@ModelAttribute Rate price, ServletWebRequest request){

    if(request.getParameterValues("idChecked") != null){
        for(String idCheckedStr : request.getParameterValues("idChecked")){
            int idrate = Integer.getInteger(idCheckedStr);
            rateRepository.deleteRate(idrate);
            } 
    }
    return "redirect:/admin/rates/prices";
}

我得到一个空指针异常:

java.lang.NullPointerException: null
at com.rentalwebs.controllers.rates.PriceListController.delete(PriceListController.java:42)

我认为这个方法应该收集值::

request.getParameterValues("idChecked")

这是表单中的一行,它使用 Thymeleaf 注释创建每个复选框:

<input type="checkbox" th:name="idChecked" th:value="${price.idrate}"/>

这是表单的视图代码:

<form th:action='@{/admin/rates/prices/delete}'
      method="POST"
      th:object="${rate}">

     <button type="submit" name='delete' value="delete"
             class='btn btn-secondary btn-sm'
             th:text="#{delete}"
             data-toggle="tooltip" data-placement="right"
             th:title="#{delete.selected}">
     </button>

     <p></p>
     <table class='table table-sm responsive'>
           <thead class='thead-default'>
                <tr>
                    <th><input type="checkbox" id="checkAll"/></th>
                    <th th:text='#{from}'></th>
                    <th th:text='#{to}'></th>
                    <th th:text='#{price}'></th>
                </tr>
           </thead>
           <tbody>
                  <tr th:each="price : ${prices}">
                      <td>
                          <input type="checkbox" th:name="idChecked" th:value="${price.idrate}"/>
                      </td>
                      <td th:text="${#temporals.format(price.datefrom, 'dd/MM/yyyy')}"></td>
                      <td th:text="${#temporals.format(price.dateto, 'dd/MM/yyyy')}"></td>
                      <td th:text="${price.price} + '&nbsp; €'"></td>                                        
                  </tr>
           </tbody>
     </table>
</form>

提前感谢您的帮助:-)

【问题讨论】:

  • 是的,ServletWebRequest会获取所有参数。但是,不要使用它。使用更多的 Spring 惯用方法。通过@RequestParam("idChecked") List&lt;String&gt; ids获取参数值列表。然而,NPE 发生在哪里?在控制器的第 42 行调用了什么方法?使用完整的堆栈跟踪更新您的问题。
  • 我建议您查看 Integer.getInteger(String s) 的作用。它不解析字符串并给你一个整数表示(“123”-> 123)。这是最有可能导致 NullPointerException 的原因,因为 getInteger 将在此处返回 null,如果您将一个为 null 的 Integer 拆箱,您将得到 NullPointerException。如果要将字符串解析为有符号十进制整数,请使用 Integer.parseInt(String s) 并注意,如果您尝试解析包含非数字字符的字符串,仍然可能出现异常 (NumberFormatException)。跨度>
  • 非常感谢@BranislavLazic 和@TommySchmidt,我已经设法用@RequestParam 而不是ServletWebRequest 解决了这个问题,并用Integer.parseInt(String s) 将字符串转换为int。

标签: spring spring-mvc checkbox spring-boot thymeleaf


【解决方案1】:

这是解决方案的代码,取自 cmets:

@PostMapping("/admin/rates/prices")
public String delete(@RequestParam("idChecked") List<String> idrates){

    if(idrates != null){
        for(String idrateStr : idrates){
            int idrate = Integer.parseInt(idrateStr);
            rateRepository.deleteRate(idrate);
            } 
    }
    return "redirect:/admin/rates/prices";
}

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2013-05-29
    • 2015-07-25
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多