Struts2 Checkbox:如何将值设置回Bean

时间:2013-10-28 09:41:58

标签: java struts2

我有一个下面的pojo表单bean类: -

class A{

int role;
List<String> roleList;
List<B> menuList;
public setMenuList(List<B> menuList)
{
    this.menuList=menuList;
}
}

我的menuList是B类,所以Following是第二个pojo类B: -

class B{
            private String displayName;
        private boolean  viewCheckBox;
        private boolean  addCheckBox;
        private boolean  editCheckBox;
        private boolean  deleteCheckBox;
        private boolean  downloadCheckBox;
        private String menuKey;
        private int menuActionFlag;
        private int menuId;
        private int menuActive;
        private int menuLevel;
            // setter and getters
}

在我的动作类中,我正在创建A类的对象并调用A的setter和getter。

public class MenuAction
{
    A a=new A();
    //getter and setter of A
    public list getAllMenus(){
     // populating menuList  from the database
    }


 public String save()
{
    a=getA();
    System.out.println("In Save"+a);
            List<B> list=a.getMenuList();
    System.out.println("MenuList is"+ list); // **  here i should get the menuList from jsp but its returning Null**
    //  code to save the changes into database

}

}

我的jsp显示一个包含许多复选框的表格形式复选框的状态在B类中,而A类包含List menuList作为属性..在jsp中我从menuList迭代并取决于B中布尔变量的状态我正在设置复选框..

     <c:forEach var="b" items="${a.menuList}"varStatus="status">
    <c:if test="${b.getMenuLevel()==2}">

    <tr>

    <td align="center">
    <c:out value="${b.isViewCheckBox()}"></c:out> 

        <c:choose>
            <c:when test="${b.isViewCheckBox()}">
            <c:out value="${b.isViewCheckBox()}"></c:out>
            <p>
            <s:checkbox name="b.viewCheckBox" id="v_%{menuKey}" fieldValue="b.viewCheckBox" value="#attr.b.viewCheckBox"/>
                                                    </p>
        </c:when>


<c:otherwise>
            <p><s:checkbox name="b.viewCheckBox" id="v_%{menuKey}" fieldValue="b.viewCheckBox" value="#attr.b.viewCheckBox"
            disabled="true" />                                          </p>                                        </c:otherwise>

        

当我点击保存时,我进入动作类的保存方法,我将menuList视为null ....我认为List是B类,这就是为什么它显示为null ..它没有得到B ...或者bean里面的menuList没有设置.. 如何解决这个问题..

1 个答案:

答案 0 :(得分:2)

好的,有很多事情需要考虑:

  1. 请不要混入GUI(struts2标签&amp; jstl)

  2. 根据行动中的预期,该名称是错误的。

  3. <s:checkbox name="b.viewCheckBox" id="v_%{menuKey}" fieldValue="b.viewCheckBox" value="#attr.b.viewCheckBox" disabled="true" />

    如果你有一个

    的setter,上面的方法就可以了

    private B b;

    但你有List<B> menuList;的设定者,因此复选框的名称应为

    <s:checkbox name="menuList[0].viewCheckBox" id="v_%{menuKey}" fieldValue="menuList[0].viewCheckBox" value="#attr.b.viewCheckBox" disabled="true" />