Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

java - Struts2 iterator how to get it to work?

I am using Struts2 <iterator> tag to display value in JSP. Method is getting called, but no results were displayed. It prints in console all the data that I need but not in JSP.

I call localhostlistAddCompany.php

What did I do wrong?

Here's the method

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>

and here is my

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The iterator tag iterates everything inside the body of the tag. No results is displayed because your iterator tag body is empty. For the iterator and other struts tags that need data to work you should populate the collection that used in the value attribute and provide a getter for the variable.

Of course this will work if you call the action first that returns a result to the JSP. In some cases if you have a validation and workflow interceptors on the stack your action class should populate the collection even if no action is executed.

For example, if after submitting a form you have validation errors and input result is returned. In this case you can make your action class to implement Preparable and move the code tho fill the list there.

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

The Company class should also have getters and setters.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...