Reputation: 21
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 EmailObject
s.
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 userEmail
s 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 userEmail
s. 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
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
Reputation: 3009
You can do following.
<s:iterator value="EmailObjects">
<s:checkbox name="emailAddressess" fieldValue="%{emailAddress}"
label="%{emailId}" >
</s:checkbox>
</s:iterator>
Upvotes: 1
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