Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring mvc复选框未设置对象_Spring_Jsp_Spring Mvc_Checkbox - Fatal编程技术网

spring mvc复选框未设置对象

spring mvc复选框未设置对象,spring,jsp,spring-mvc,checkbox,Spring,Jsp,Spring Mvc,Checkbox,大家好,我是SpringMVC的新手,正在尝试实现一个表单:checkbox标签,遇到了一些问题。我在谷歌上搜索的所有例子都是使用字符串的,我想使用对象,所以我希望有人能提供建议 我在DTO中设置了一个对象列表,如下所示: TestDTO private List<Barrier> barriers; public List<Barrier> getBarriers() { return barriers; } public void

大家好,我是SpringMVC的新手,正在尝试实现一个表单:checkbox标签,遇到了一些问题。我在谷歌上搜索的所有例子都是使用字符串的,我想使用对象,所以我希望有人能提供建议

我在DTO中设置了一个对象列表,如下所示:

TestDTO

private List<Barrier> barriers;

public List<Barrier> getBarriers() {
        return barriers;
    }

    public void setBarriers(List<Barrier> barriers) {
        this.barriers = barriers;
    }
<form:checkboxes path="barriers" items="${testDto.barriers}" element="label class='block-label'" 
                            itemLabel="barrier"/>
在我的JSP中,我使用form:checkbox标记,如下所示:

TestDTO

private List<Barrier> barriers;

public List<Barrier> getBarriers() {
        return barriers;
    }

    public void setBarriers(List<Barrier> barriers) {
        this.barriers = barriers;
    }
<form:checkboxes path="barriers" items="${testDto.barriers}" element="label class='block-label'" 
                            itemLabel="barrier"/>
但这也不起作用

这被包装在from元素中

<form:form method="post" accept-charset="UTF-8" action="${action}"
        onsubmit="return checkAndSend()" id="create"
        novalidate="" modelAttribute="testDto">
  • 当我单击submit并查看testDto时,我的barriers对象列表为空。如何获取表示要在testDto上设置的对象的复选框 任何提示和建议都将不胜感激

    谢谢

    更新:

    谢谢你的指点。我同意以下观点。你的建议有帮助

    我在控制器中创建了以下内容

    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        binder.registerCustomEditor(Barrier.class, new BarrierPropertyEditor(barrierService));
    }
    
    然后添加了一个类来进行转换

    public class BarrierPropertyEditor extends PropertyEditorSupport {
    
        private BarrierService barrierService;
    
        public BarrierPropertyEditor(BarrierService barrierService) {
            this.barrierService = barrierService;
        }
    
        @Override
        public void setAsText(String text) {
            Barrier b = barrierService.findById(Integer.valueOf(text));
            setValue(b);
        }
    
    }
    
    这将设置我的DTO上的屏障对象

    (很抱歉有上限)它无法解决为什么在初始加载时选中复选框


    您知道如何在初始加载时取消选中复选框吗?

    您可以在控制器中使用@modeldattribute来提供复选框中的值列表

    @ModelAttribute("barrierList")
    public List<Barrier> populateBarrierList() {
        List<Barrier> barrierList = dataService.getBarriers();
        for(Barrier barrier: barrierList )
        {
            barrierList.add(barrier);
        }       
        return barrierList ;
    }
    
    @modeldattribute(“barrierList”)
    公共列表populateBarrierList(){
    List barrierList=dataService.getBarriers();
    用于(障碍物:障碍物清单)
    {
    增加(障碍物);
    }       
    返回大律师;
    }
    
    在JSP中,使用以下命令:

    <form:checkboxes path="barriers" items="${barrierList}" element="label class='block-label'"  itemLabel="barrier"/>
    
    
    
    请查看我的更新。您知道为什么在初始加载时选中该复选框吗?这是因为您正在命令对象testDto中设置屏障-->savedRecord.setBarriers(dataService.getBarriers())。不要这样做,而是如上所述使用@modeldattribute(“barrierList”)。将上述答案标记为正确,以避免默认情况下未选中复选框。有关如何在DTO上设置复选框值的信息,请参见我的更新
    <form:checkboxes path="barriers" items="${barrierList}" element="label class='block-label'"  itemLabel="barrier"/>