如何使用实体列表来默认默认检查值支撑2复选框列表

问题描述 投票:1回答:1

我正在尝试使用struts checkboxlist默认检查一些值:

CheckBoxListAction.class

    public class CheckBoxListAction extends ActionSupport{
         private List<Categories> listCategories;
         private List<Categories> categories;

         public void setListCategories(List<Categories> listCategories) {
              this.listCategories = listCategories;
         }

        public List<Categories> getListCategories() {
             listCategories = new ArrayList<>();
             listCategories.add(new Categories(1, "AAA"));
             listCategories.add(new Categories(2, "BBB"));
             listCategories.add(new Categories(3, "CCC"));
             listCategories.add(new Categories(4, "DDD"));
             return listCategories;
        }

        public List<Categories> getCategories() {
             return categories;
        }

        public void setCategories(List<Categories> categories) {
             this.categories = categories;
        }

        public List<Categories> getDefaultCategories(){
             List<Categories> vList = new ArrayList<>();
             vList.add(new Categories(1, "AAA"));
             vList.add(new Categories(2, "BBB"));
             return vList;
        }

    }

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">

   <action name="checkBoxListAction"
         class="demo.example.CheckBoxListAction">
    <result>jsp/page.jsp</result>
   </action>
  </package>
</struts>

page.jsp

<s:checkboxlist name="categories" label="Categories"
     list="listCategories" listKey="id" listValue="label" value="defaultCategories"/>

与:[ ]AAA [ ]BBB [ ]CCC [ ]DDD的较好结果

<input type="checkbox" name="categories" value="1" id="checkboxlist_categories-1"/>
<label for="checkboxlist_categories-1" class="checkboxLabel">AAA</label>
<input type="checkbox" name="categories" value="2" id="checkboxlist_categories-2"/>
<label for="checkboxlist_categories-2" class="checkboxLabel">BBB</label>
<input type="checkbox" name="categories" value="3" id="checkboxlist_categories-3"/>
<label for="checkboxlist_categories-3" class="checkboxLabel">CCC</label>
<input type="checkbox" name="categories" value="4" id="checkboxlist_categories-4"/>
<label for="checkboxlist_categories-4" class="checkboxLabel">DDD</label>
<input type="hidden" id="__multiselect_customer-categories-new_categories" name="__multiselect_categories" value="" />

我期望:[ X ]AAA [ X ]BBB [ ]CCC [ ]DDD,但是未将“ AAA”和“ BBB”检查为默认值。

我已经遵循了本教程:struts2-checkboxlist-multiple-check-boxes-examples,并检查了stackoverflow中的一些常见问题,但无法弄清楚出了什么问题。

非常感谢您的帮助,对不起英语不好。

java jsp struts2 entity checkboxlist
1个回答
0
投票

抱歉打扰您,

经过无数次尝试,解决方案是在价值方面,而不是实体。

public List<Integer> getDefaultCategories(){
        List<Integer> vInt = new ArrayList<>();
        vInt.add(1);
        vInt.add(2);
        return vInt;
}

谢谢您的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.