Ankit Duggal
Ankit Duggal

Reputation: 55

Struts2 Checkbox: How to set value back to Bean

I have a folowing pojo form bean class:-

class A{

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

My menuList is of type B so the Following is the second pojo class 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
}

IN my action class i am creating object of class A and calling setter and getter of A.

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

}

}

My jsp is showing a tabular form containg many checkboxes state of checkboxes are in class B and class A contains List menuList as an attribute.. In jsp i am iterating from the menuList and depending upon the status of the boolean var in B i am setting the checkboxes..

     <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>

While I click on save i go in save method of the action class where i am getting menuList as null.... I think the List is of type B that's why its showing null.. its not getting B... or maybe menuList inside bean is not getting set.. How to solve this problem..

Upvotes: 0

Views: 3385

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

Ok, there are various things to consider :

  1. Please don't mix in the GUI (struts2 tags & jstl)

  2. The name is wrong according to what is expected in the action.

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

The above will work if you have a setter for

private B b;

But you're having the setter for List<B> menuList; and hence the name of the checkbox should be

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

Upvotes: 2

Related Questions