我正在尝试默认选中一些带有支柱复选框列表的值:
复选框列表操作.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;
}
}
支柱.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>
页.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-复选框列表-多个复选框-示例 并检查堆栈溢出中的一些常见问题解答,但无法找出问题所在。
非常感谢您的帮助,对不起英语不好。
对不起,大家打扰了你,
经过多次尝试,解决方案是在价值方面,而不是实体。
public List<Integer> getDefaultCategories(){
List<Integer> vInt = new ArrayList<>();
vInt.add(1);
vInt.add(2);
return vInt;
}
谢谢你的帮助。