Struts2迭代器如何让它工作? [英] Struts2 iterator how to get it to work?

查看:87
本文介绍了Struts2迭代器如何让它工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我调用 localhost \listAddCompany.php



我做错了什么?

以下是方法

listAllCompanys()
$ b

 私人列表<公司> listAllCompanys; 
getter和setter方法...

公共字符串listAllCompanys()抛出异常{

CompanyDaoHibernate道=新CompanyDaoHibernate();
listAllCompanys = dao.getListOfCompanies();
System.out.println(从CompanyManagmentAction打印...);

返回SUCCESS;

struts.xml $ b

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

这里是我的

companyAddUpdate.jsp
$ b

 < h1>添加/更新公司< / h1> 

<! - // id,Company_name,Address,Email,Website,Phone_number,Comment,Fax - >

< s:actionerror />
< s:textfield name =company.emaillabel =Email/>
< s:submit value =Register/>

< / s:form>

< h2>通讯录< / h2>
< s:iterator value =listAllCompanysvar =company>
< / s:iterator>< table>
< tbody>< tr>
< th>公司名称< / th>
< th>地址< / th>
< th>电子邮件< / th>
< th>网站< / th>
< th>电话号码< / th>
< th>评论< / th>
< th>传真< / th>
< / tr>
< tr>
< td>< s:property value =companyName>< / s:property>< / td>
< td>< s:属性值=地址>< / s:属性>< / td>
< td>< s:属性值=电子邮件>< / s:属性>< / td>
< td>< s:property value =website>< / s:property>< / td>
< td>< s:属性值=phoneNumber>< / s:属性>< / td>
< td>< s:属性值=评论>< / s:属性>< / td>
< td>< s:属性值=传真>< / s:属性>< / td>

< / tr>

< / tbody>< / table>


解决方案

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

当然,如果您首先调用将操作返回给JSP的操作,这将起作用。在某些情况下,如果堆栈中存在验证工作流程拦截器,则操作类应该填充集合,即使没有行动被执行。例如,如果在提交表单后发现验证错误,并返回 input 结果。在这种情况下,你可以让你的动作类来实现 可预备 ,然后将代码移到那里。

public class CompanyAction extends ActionSupport implements Prepaable {

private List<公司> listAllCompanys;

//吸气剂和吸附剂...​​

公共清单<公司> getListAllCompanys(){
return listAllCompanys;

$ b @Override
public void prepare()抛出异常{
CompanyDaoHibernate dao = new CompanyDaoHibernate();
listAllCompanys = dao.getListOfCompanies();
System.out.println(从+ getClass()填充listAllCompanysgetSimpleName()+size:+ listAllCompanys.size());

}

公共字符串listAllCompanys()抛出异常{
的System.out.println( 操作 + ActionContext.getContext()。的getName()+ 叫做);
返回SUCCESS;

公司类在JSP中:

 

< H2>联系和LT; / H2>
< table>
< thead>
< tr>
< th>公司名称< / th>
< th>地址< / th>
< th>电子邮件< / th>
< th>网站< / th>
< th>电话号码< / th>
< th>评论< / th>
< th>传真< / th>
< / tr>
< / thead>
< tbody>
< s:iterator value =listAllCompanys>
< tr>
< td>< s:property value =companyName/>< / td>
< td>< s:属性值=地址/>< / td>
< td>< s:property value =email/>< / td>
< td>< s:property value =website/>< / td>
< td>< s:property value =phoneNumber/>< / td>
< td>< s:属性值=评论/>< / td>
< td>< s:属性值=传真/>< / td>
< / tr>
< / s:iterator>
< / tbody>
< / table>


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 localhost\listAddCompany.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>

解决方案

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>

这篇关于Struts2迭代器如何让它工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆