简体   繁体   English

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

[英]Java Spring with thymeleaf checkbox non checked is Empty instead of false

I have checkbox inside of my form : 我的表单内有复选框:

  <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>

This checkbox by default should be on since search object has this value assigned as true. 默认情况下,此复选框应该处于启用状态,因为搜索对象将此值分配为true。 This works fine. 这很好。 What i expected from thsi checkbox is to send either true or false when its checked/unchecked. 我期望从此复选框发出的是,如果选中/未选中,则发送true或false。 What i got now is True if its checked and nothing when not checked. 如果得到检查,我现在得到的是True,如果未检查则什么也没有。 That his a bit of an issue for me : 那对我来说有点问题:

In 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;
}

Whole point of this form is to get user select what parts of the object he wants to search : 此表单的全部目的是让用户选择他要搜索的对象的哪些部分: 在此处输入图片说明

So if he unselects checkbox and i dont send FALSE in optional, I run into problem. 因此,如果他取消选择复选框,而我没有以可选方式发送FALSE,则会遇到问题。

When user initialy goes into the site, he doesnt send any of these parameters so it behaves fine by setting everything to true. 当用户最初进入站点时,他不会发送任何这些参数,因此通过将所有内容都设置为true可以使其工作正常。 Bud once he initiates search and deselects parts he doesnt wanna search in, he still ends up with defaults and searches in everything. 芽一旦启动搜索并取消选择了他不想搜索的部分,他仍然会得到默认值并搜索所有内容。

So is there a simple way of sending Optional FALSE if checkbox value is not selected? 因此,如果未选中复选框值,是否有一种简单的发送可选FALSE的方法?

Note : 注意 :

  • By simple i mean without creating additional endpoint for this form search and no Javascript :) 简单地说,我的意思是无需为此表单搜索创建其他端点并且没有Javascript :)

  • And yes the form is under GET request 是的,表格在GET请求下

No matter what you do , only checked checkboxes are sent to server, not unchecked one.You have to code it in server to handle unchecked checkboxes. 无论您做什么,都只会将选中的复选框发送到服务器,而不是未选中的复选框。您必须在服务器中对其进行编码以处理未选中的复选框。

https://www.w3.org/TR/html401/interact/forms.html#form-controls 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. 设置控制元素的选中属性时,开关为“ on”。 When a form is submitted, only "on" checkbox controls can become successful. 提交表单后,只有“启用”复选框控件才能成功。

But with the thymeleaf you can try like below , 但是,有了百里香叶,您可以像下面这样尝试,

<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>

which will produce below html , 这将在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. 不用担心那些带有name =“ _ features”的隐藏输入:它们会自动添加,以避免浏览器在提交表单时未将未经检查的复选框值发送到服务器的问题。

More on, 更多关于,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM