复选框值在错误时不会绑定到对象中? [英] Checkbox values do not bind into object when false?

查看:84
本文介绍了复选框值在错误时不会绑定到对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ModelAttribute 在Spring Web应用程序中绑定对象。



一旦我注意到,布尔值A为true,如果我们取消选中A的复选框,则其值不会更新。

例如,我有一个Lesson对象,它具有属性活动=真。
在编辑课程视图中,我制作了一个复选框,将其绑定到活动。如果选中该复选框(绑定对象反映更改),事情就会正常运行,但如果我们取消选中复选框,对象课程将不会更改。



进一步的研究告诉我这是因为复选框的值可能不会被浏览器提交(这是HTML的设计)。因此,我必须使用丑陋的 request.getParameter 来检查是否设置了该值。



我刚刚通过这个问题 ,我发现asp.net mvc提供了一种更优雅的解决方法。我认为Spring必须提供类似的东西。有人知道如何做到这一点吗?



以下是我的代码:



控制器代码:

  @RequestMapping(value =/ test,method = RequestMethod.POST)
public String processEditLesson(@Valid Lesson lesson,BindingResult bindingResult,Model model){
System.out.println(Lesson is active:+ lesson.isActive()); //即使复选框未设置,仍然为true

//当前解决方法
String isActive = request.getParameter(active);
if(StringUtils.isNotNullOrEmpty(isActive)){
lesson.setActive(true);
} else {
lesson.setActive(false);
}
...
}

查看代码:

 < form id =lessonclass =EditorFormaction =$ {rc.getContextUrl('/ test.html ')}method =post> 

< fieldset>
< legend>< @ spring.message code =lesson.edit/>< / legend>
<@ spring.formHiddenInputlesson.id/>
<@ spring.formHiddenInputlesson.studio.id/>

< div class =Entry>
< label for =name>< @ spring.message code =lesson.message/>< / label>
<@ spring.formInputlesson.message/>
< span class =ErrorMessage>< @spring.showErrors< br /> />< /跨度>
< / div>

< input type =checkboxname =activechecked =checked/>
< label for =active> $ {rc.getMessage('lesson.active')}< / label>

< input type =submitvalue =< @spring.message code ='common.update'/> />
< / fieldset>
< / form>


解决方案

Spring有一个内置的解决方法。



只需将这个额外的隐藏字段添加到表单中即可:

< input type =hidden value =onname =_ active/>



带有前导下划线的参数是某种标记,存在具有相同名称但没有下划线的复选框参数。

如果只有 _active = on ,Spring现在应该将 lesson.active 设置为false。 code>被提交。


I used ModelAttribute to bind object in Spring web application.

Once I notice that, in case an object has an boolean value A is true, its value will not be updated if we uncheck A's checkbox.

For example, I have a Lesson object which has the attribute "active" = true. In "Edit Lesson" view, I make a checkbox which bind into "active". Things work well if the checkbox is checked (the binding object reflect the changes), but the object lesson will not change if we un-check the checkbox.

Further study tells me that's because the checkbox value may not get submitted by browser (this is an in-design of HTML). So I have to use the ugly request.getParameter to check if the value is set.

I just come by this question, and I see that asp.net mvc provide a way to work around it more elegantly. I think Spring must provide something similarly. Does anyone know how to do that?

Following is my code:

Controller code:

    @RequestMapping(value="/test", method = RequestMethod.POST)
    public String processEditLesson(@Valid Lesson lesson, BindingResult bindingResult, Model model) {
        System.out.println("Lesson is active: " + lesson.isActive()); // still "true" even if the checkbox is unset

        // Current work-around
        String isActive = request.getParameter("active");
        if (StringUtils.isNotNullOrEmpty(isActive)) {
            lesson.setActive(true);
        } else {
            lesson.setActive(false);
        }
        ...
    }

View code:

<form id="lesson" class="EditorForm" action="${rc.getContextUrl('/test.html')}" method="post" >

    <fieldset>
        <legend><@spring.message code="lesson.edit"/></legend>
        <@spring.formHiddenInput "lesson.id" />
        <@spring.formHiddenInput "lesson.studio.id" />

        <div class="Entry">
            <label for="name"><@spring.message code="lesson.message"/></label>
            <@spring.formInput "lesson.message" />
            <span class="ErrorMessage"><@spring.showErrors "<br/>" /></span>
        </div>

        <input type="checkbox" name="active" checked="checked" />
        <label for="active">${rc.getMessage('lesson.active')}</label>

        <input type="submit" value="<@spring.message code='common.update' />" />
    </fieldset>
</form>

解决方案

Spring has a built in workaround.

Simply add this additional hidden field to the form:

<input type="hidden" value="on" name="_active"/>

The parameter with a leading underscore is some kind of marker, to indicate the existence of a checkbox parameter with the same name, but without the underscore.

Spring should now set lesson.active to false if only _active=on is submitted.

这篇关于复选框值在错误时不会绑定到对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆