Struts 2从对象的Arraylist填充复选框

时间:2013-11-09 15:18:37

标签: java jsp checkbox struts2 ognl

我确信我正在做一些愚蠢的事情,但过去几天我一直遇到困难,试图让我的checkboxes填入一个超过ArrayList的迭代器中对象。

这是我的目标:

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

在我的操作课程中,我创建了上述ArrayList的{​​{1}}。

在我的JSP页面上,我可以通过EmailObjects获取复选框(但这对我不利,因为我希望它是垂直的,不想搞乱Struts样式)

checkboxlist

我还可以遍历arraylist userEmails并显示值:

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

但在迭代<s:iterator value="userEmails" var="thisEmailData"> <s:property value="emailId"/> <s:property value="emailAddress"/> </s:iterator> 时,我无法显示emailId。我尝试了所有这些:

ArrayList userEmails

从上面开始,值字段永远不会填充 <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> 的值。

我知道我遗漏了一些基本的东西。它是什么?

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作。

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

答案 1 :(得分:0)

尝试在动作类中使用hashmaps。示例

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

在你的jsp文件中使用jstl标签库 -

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

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

答案 2 :(得分:0)

您忘记了与复选框对应的简单含义。复选框仅选中或取消选中两个状态。如果选中了复选框,则会在取消选中未选中时将checked="checked"添加到input标记。您如何看待哪种数据类型变量适合表示这些状态?我认为你得到了它,它是一个布尔类型。现在让我们看一下struts站点上example的checkbox标签。他们说value="aBoolean"。不幸的是,这需要更多描述,如

Boolean aBoolean; 

此变量通过OGNL进行评估,如果它具有true值,则会添加checked="checked",您可以看到检查符号,否则将被删除。