user2972139
user2972139

Reputation: 21

Populating checkboxes from ArrayList of Objects in Struts 2

I'm sure that I'm doing something dumb, but I've been having difficulty over the last couple of days trying to get my checkboxes filled out inside <s:iterator> tag that goes over an ArrayList of Object.

Here is my Object:

public class EmailObject {
int emailId;
String emailAddress;

public int getEmailId() {
    return emailId;
}
public void setEmailId(int emailId) {
    this.emailId = emailId;
}
public String getEmailAddress() {
    return emailAddress;
}
public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
}
}

In my action class, I create an ArrayList of the above EmailObjects.

On my JSP page, I can get checkboxes through a checkboxlist (but this isn't good for me, because I want it to be vertical and don't want to mess with the Struts styles)

<s:checkboxlist name="selectedEmails" list="userEmails" listValue="emailAddress" listKey="emailId" />

I can also iterate over the ArrayList of userEmails and display the values:

<s:iterator value="userEmails" var="thisEmailData">
<s:property value="emailId"/>
<s:property value="emailAddress"/>
</s:iterator>

But, I can't get it to display the emailId when iterating over the ArrayList of userEmails. I tried all of these:

    <s:iterator value="userEmails" var="thisEmailData">
<tr><td><s:property value="emailId"/></td></tr>

<tr><td>
    <s:checkbox fieldValue="%{#emailId}" name="emailAddressesCB"   theme="simple"  ></s:checkbox>
    <s:checkbox fieldValue="#emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="thisEmailData.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="userEmails.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="#thisEmailData.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="#userEmails.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="%{#thisEmailData.emailId}" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="%{#userEmails.emailId}" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox fieldValue="emailId" name="emailAddressesCB"   theme="simple"  ></s:checkbox>
    
    <s:checkbox value="%{#emailId}" name="emailAddressesCB"   theme="simple"  ></s:checkbox>
    <s:checkbox value="#emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="thisEmailData.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="userEmails.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="#thisEmailData.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="#userEmails.emailId" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="%{#thisEmailData.emailId}" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="%{#userEmails.emailId}" name="emailAddressesCB"   theme="simple" ></s:checkbox>
    <s:checkbox value="emailId" name="emailAddressesCB"   theme="simple"  >
     </s:checkbox>
     <s:property value="emailAddress"/>
</td></tr>
</s:iterator>
</td></tr>
</s:iterator>

From the above, the value field is never filled with the value of the emailId.

I know, I'm missing something basic. What is it?

Upvotes: 2

Views: 4854

Answers (3)

Roman C
Roman C

Reputation: 1

You forgot a simple meaning that corresponds to checkboxes. The checkboxes has only two states checked or unchecked. If checkbox is checked then checked="checked" is added to the input tag, when unchecked is removed. How do you think which data type variable is appropriate to represent these states? I think you got it, it's a boolean type. Now lets look at the example of checkbox tag on the struts site. They say value="aBoolean". Unfortunately this required more description like

Boolean aBoolean; 

This variable is evaluated via OGNL and if it has true value then checked="checked" is added and you can see the check sign, otherwise removed.

Upvotes: 0

Harsh
Harsh

Reputation: 3009

You can do following.

<s:iterator value="EmailObjects">       
<s:checkbox name="emailAddressess"  fieldValue="%{emailAddress}" 
label="%{emailId}" >
</s:checkbox>
</s:iterator>

Upvotes: 1

CrazyAboutTechnology
CrazyAboutTechnology

Reputation: 31

Try using hashmaps instead in the action class. Example

Hashmap<int,String> emailobjMap = new Hashmap<int,String>()
    emailobjMap.put(emailId,emailAddress);
    request.setAttribute(EMAILIST, emailobjMap); 

And in your jsp file using jstl tag library-

<g:forEach var="list" items="${EMAILIST}">`

    <g:out value="${list.key}" />
    <g:out value="${list.value}" />
</g>

Upvotes: 0

Related Questions