gpt4 book ai didi

java - Struts2迭代器如何让它工作?

转载 作者:行者123 更新时间:2023-12-01 11:51:30 24 4
gpt4 key购买 nike

我正在使用Struts2 <iterator>标签在 JSP 中显示值。正在调用方法,但未显示结果。它在控制台中打印我需要的所有数据,但不在 JSP 中打印。

我调用localhost\listAddCompany.php

我做错了什么?

方法如下

<强> listAllCompanys() :

private List<Company> listAllCompanys;
Getters and setters...

public String listAllCompanys() throws Exception {

CompanyDaoHibernate dao = new CompanyDaoHibernate();
listAllCompanys = dao.getListOfCompanies();
System.out.println("Printing from CompanyManagmentAction...");

return SUCCESS;
}

<强> struts.xml :

    <action name="listAddCompany" class="com.handyman.web.CompanyManagementAction"
method="listAllCompanys">
<result name="success">companyAddUpdate.jsp</result>
</action>

这是我的

<强> companyAddUpdate.jsp :

<h1>Add / Update Company</h1>

<s:form action="newCompany">
<!-- // id, Company_name, Address, Email, Website, Phone_number, Comment, Fax -->

<s:actionerror/>
<s:textfield name="company.companyName" label="Company's name" />
<s:textfield name="company.address" label="Address" />
<s:textfield name="company.email" label="Email" />
<s:textfield name="company.website" label="Website" />
<s:textfield name="company.phoneNumber" label="Phone Number" />
<s:textfield name="company.comment" label="Comment" />
<s:textfield name="company.fax" label="Fax" />
<s:submit value="Register" />

</s:form>

<h2>Contacts</h2>
<s:iterator value="listAllCompanys" var="company">
</s:iterator><table>
<tbody><tr>
<th>Company's name</th>
<th>Address</th>
<th>Email</th>
<th>Website</th>
<th>Phone Number</th>
<th>Comment</th>
<th>Fax</th>
</tr>
<tr>
<td><s:property value="companyName"></s:property></td>
<td><s:property value="address"></s:property></td>
<td><s:property value="email"></s:property></td>
<td><s:property value="website"></s:property></td>
<td><s:property value="phoneNumber"></s:property></td>
<td><s:property value="comment"></s:property></td>
<td><s:property value="fax"></s:property></td>

</tr>

</tbody></table>

最佳答案

迭代器标签迭代标签主体内的所有内容。由于迭代器标记正文为空,因此未显示任何结果。对于迭代器和其他需要数据才能工作的 struts 标签,您应该填充 value 属性中使用的集合,并为变量提供 getter。

当然,如果您首先调用将结果返回到 JSP 的操作,那么这将起作用。在某些情况下,如果堆栈上有validationworkflow拦截器,即使没有执行任何操作,您的操作类也应该填充集合。

例如,如果提交表单后出现验证错误,并且返回input结果。在这种情况下,您可以使您的操作类实现 Preparable并将代码移动到那里填充列表。

public class CompanyAction extends ActionSupport implements Preparable {

private List<Company> listAllCompanys;

//Getters and setters...

public List<Company> getListAllCompanys() {
return listAllCompanys;
}

@Override
public void prepare() throws Exception {
CompanyDaoHibernate dao = new CompanyDaoHibernate();
listAllCompanys = dao.getListOfCompanies();
System.out.println("Populated listAllCompanys from " +getClass().getSimpleName()+ " size: " +listAllCompanys.size());

}

public String listAllCompanys() throws Exception {
System.out.println("The action " + ActionContext.getContext().getName()+ " is called");
return SUCCESS;
}

Company 类还应该有 getter 和 setter。

在 JSP 中:

<h2>Contacts</h2>
<table>
<thead>
<tr>
<th>Company's name</th>
<th>Address</th>
<th>Email</th>
<th>Website</th>
<th>Phone Number</th>
<th>Comment</th>
<th>Fax</th>
</tr>
</thead>
<tbody>
<s:iterator value="listAllCompanys">
<tr>
<td><s:property value="companyName"/></td>
<td><s:property value="address"/></td>
<td><s:property value="email"/></td>
<td><s:property value="website"/></td>
<td><s:property value="phoneNumber"/></td>
<td><s:property value="comment"/></td>
<td><s:property value="fax"/></td>
</tr>
</s:iterator>
</tbody>
</table>

关于java - Struts2迭代器如何让它工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com