Java Spring 与 thymeleaf 复选框未选中时为空而不是 false

标签 java spring checkbox thymeleaf

我的表单中有复选框:

  <div class="form-check form-check-inline">
                        <input type="checkbox" th:field="${search.titleIncl}" />
                        <label class="form-check-label" th:for="${#ids.next('covered')}">Title</label>
                    </div>

默认情况下应启用此复选框,因为搜索对象已将此值指定为 true。这很好用。我对这个复选框的期望是在选中/取消选中时发送 true 或 false。如果检查了,我现在得到的是 True,如果没有检查,则什么也没有。这对我来说有点问题:

在 Controller 中:

@GetMapping(value = Utils.MAPPING_INDEX)
public ModelAndView index(@RequestParam("titleIncl") Optional<Boolean> titleIncl) {
    ...calling fillSearch inside of body....
}


private Search fillSearch(Optional<Boolean> titleIncl..) {

        Search search = new Search();

        //Get seach value if nothing provided give me default.
        search.setTitleIncl(titleIncl.orElse(search.isTitleIncl()));
        ...

        return search;
}

此表单的重点是让用户选择他想要搜索的对象的哪些部分: enter image description here

因此,如果他取消选中复选框,并且我没有在可选中发送 FALSE,我就会遇到问题。

当用户最初进入网站时,他不会发送任何这些参数,因此通过将所有内容设置为 true ,它会表现良好。 巴德,一旦他启动搜索并取消选择他不想搜索的部分,他最终仍然会使用默认值并搜索所有内容。

那么,如果未选中复选框值,是否有一种简单的方法来发送Optional FALSE?

注意:

  • 简单地说,我的意思是无需为此表单搜索创建额外的端点,也无需使用 Javascript :)

  • 是的,表单位于 GET 请求下

最佳答案

无论你做什么,只有选中的复选框才会发送到服务器,而不是未选中的复选框。你必须在服务器中对其进行编码以处理未选中的复选框。

https://www.w3.org/TR/html401/interact/forms.html#form-controls

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.

但是对于 thymeleaf ,你可以尝试如下,

<ul>
  <li th:each="feat : ${allFeatures}">
    <input type="checkbox" th:field="*{features}" th:value="${feat}" />
    <label th:for="${#ids.prev('features')}" 
           th:text="#{${'seedstarter.feature.' + feat}}">Heating</label>
  </li>
</ul>

这将产生以下 html ,

<ul>
  <li>
    <input id="features1" name="features" type="checkbox" value="SEEDSTARTER_SPECIFIC_SUBSTRATE" />
    <input name="_features" type="hidden" value="on" />
    <label for="features1">Seed starter-specific substrate</label>
  </li>
  <li>
    <input id="features2" name="features" type="checkbox" value="FERTILIZER" />
    <input name="_features" type="hidden" value="on" />
    <label for="features2">Fertilizer used</label>
  </li>
  <li>
    <input id="features3" name="features" type="checkbox" value="PH_CORRECTOR" />
    <input name="_features" type="hidden" value="on" />
    <label for="features3">PH Corrector used</label>
  </li>
</ul>

Don’t worry about those hidden inputs with name="_features": they are automatically added in order to avoid problems with browsers not sending unchecked checkbox values to the server upon form submission.

更多信息,

https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#checkbox-fields

关于Java Spring 与 thymeleaf 复选框未选中时为空而不是 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363810/

相关文章:

java - Spring BeanFactory 和类型搜索

javascript - 如何使用 JS 或 jQuery 从组复选框名称数组中检索引用

javascript - 使用复选框将 JSON 结果解析为对象

java - 按下完成后,Eclipse editText 移除焦点(光标)

java - Spring MVC-从 Controller 返回字符串值

java - 服务器日志在哪里

jQuery 复选框组

在 try catch block 中执行 SQL 查询时出现 java.lang.NullPointerException 错误

java - 在 Java 中赋值?

java - 没有 Spring 上下文的集成测试