3

我确定我在做一些愚蠢的事情,但在过去的几天里,我一直很难在一个迭代器中填写我的复选框ArrayList,该迭代器超过了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;
}
}

在我的动作类中,我创建了一个ArrayList上述EmailObjects。

在我的 JSP 页面上,我可以通过 a 获取复选框checkboxlist(但这对我不利,因为我希望它是垂直的并且不想与 Struts 样式混淆)

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

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

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

从上面可以看出, value 字段永远不会填充emailId.

我知道我缺少一些基本的东西。它是什么?

4

3 回答 3

1

您可以执行以下操作。

<s:iterator value="EmailObjects">       
<s:checkbox name="emailAddressess"  fieldValue="%{emailAddress}" 
label="%{emailId}" >
</s:checkbox>
</s:iterator>
于 2014-06-24T09:08:53.693 回答
0

您忘记了与复选框相对应的简单含义。复选框只有选中或未选中两种状态。如果选中复选框,则将checked="checked"其添加到input标签中,未选中时将其删除。您认为哪种数据类型变量适合表示这些状态?我想你明白了,它是一个布尔类型。现在让我们看一下struts 网站上的复选框标记示例。他们说value="aBoolean"。不幸的是,这需要更多描述,例如

Boolean aBoolean; 

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

于 2013-11-09T17:48:48.330 回答
0

尝试在操作类中使用哈希图。例子

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>
于 2013-11-09T15:38:34.400 回答