Html,thymleaf radiobutton th:field =“”如果数据为空则失败

时间:2017-12-11 10:18:25

标签: html input radio-button thymeleaf

我想自动填写收据数据。如果我的收据不为空,则应选择正确的性别到带百里香的单选按钮。如果收据不为空,则没有错误,但如果收货为空,则抛出错误

<div class="form-group">
            <div id="genderReceipt">
                <label for="genderMaleReceipt" class="radio-inline">
                    <input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" th:field="*{receipt.gender}"/>
                    Herr
                </label>
                <label for="genderFemaleReceipt" class="radio-inline">
                    <input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" th:field="*{receipt.gender}"/>
                    Frau
                </label>
            </div>
            <label for="genderReceipt" class="error" style="display: none;"> Bitte gib eine Anrede an.</label>
</div>

结果应如下:如果收到== null两者都未选中...如果收到!= null则检查正确的性别

错误日志说:

  

java.lang.IllegalStateException:BindingResult和bean名称'receipt'的普通目标对象都不可用作请求属性

1 个答案:

答案 0 :(得分:0)

我对此问题的解决方案是复制代码并在示例

中插入th:if语句

<div th:if="${receipt} == null">
                <label for="genderFemaleReceipt" class="radio-inline">
                    <input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" />
                    Herr
                </label>
                <label for="genderFemaleReceipt" class="radio-inline">
                    <input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" />
                    Frau
                </label>
            </div>
            <div th:if="${receipt} != null">
                <label for="genderFemaleReceipt" class="radio-inline">
                    <input type="radio" name="genderReceipt" id="genderMaleReceipt" value="MALE" th:field="*{receipt.gender}"/>
                    Herr
                </label>
                <label for="genderFemaleReceipt" class="radio-inline">
                    <input type="radio" name="genderReceipt" id="genderFemaleReceipt" value="FEMALE" th:field="*{receipt.gender}"/>
                    Frau
                </label>
            </div>

我知道这不是最佳做法;(但我不知道另一种方法。