简体   繁体   English

在Struts2视图中显示列表

[英]Show List in Struts2 view

I am using struts2 as MVC framework,but i had an issue,my problem is in Action,I defined a model which called "Company",Its definition is as following: 我使用struts2作为MVC框架,但我有一个问题,我的问题是在Action中,我定义了一个名为“Company”的模型,其定义如下:

class Company
{
  private String name;
  private String address;
  ...
  private List<Staff>staffs;
  ...
}

class Staff
{
   private String name;
   private int age;
   ....
}

The Action is as following 行动如下

class Action
{
  private Company company;
  public String execute(){

  }
}

How to show the whole company and staff info in view(The view is JSP file) ?what i expected in UI is: 如何在视图中显示整个公司和员工信息(视图是JSP文件)?我在UI中的期望是:

Company info
  company name                company address...
Staff 1 info
   staff name                 staff age...
Staff 2 info
   staff name                 staff age...

How to define the jsp file?even though company info could be shown by using OGNL,but how about staff info,As it is a List and its number is uncertain 如何定义jsp文件?即使公司信息可以通过使用OGNL显示,但员工信息如何,因为它是一个列表,其数量不确定

Someone told me i could use iterator,but my requirement is: Beside show the staff infomation in UI,I also hope to let the staff could be editable,eg maybe i will define "form" as following 有人告诉我,我可以使用迭代器,但我的要求是:除了在UI中显示员工信息,我也希望让员工可以编辑,例如,我可能会定义“表单”如下

<s:form> 
    Company info: Company Name:<s:input name="company.name"/> Company Address:<s:input name="company.name"/> 
   Staff 1 info: .... 
   Staff 2 info: ... 
 <s:form> 

Is it possible to define the staff info with "form" or "input"? 是否可以使用“表单”或“输入”定义员工信息?

You can use Struts2 Iterator tag to iterator over the list and show all information on your JSP page 您可以使用Struts2 Iterator标记对列表进行迭代,并显示JSP页面上的所有信息

Iterator will iterate over a value. 迭代器将迭代一个值。 An iterable value can be any of: java.util.Collection, java.util.Iterator 可迭代值可以是以下任何值: java.util.Collection, java.util.Iterator

For more details refer to Iterator tag example . 有关更多详细信息,请参阅Iterator 标记示例

To get the company name and address just do 要获得公司名称和地址

 <s:property value="company.name"/>
 <s:property value="company.address"/>

And to get list of staffs use struts iterator tag 并获得员工列表使用struts迭代器标记

<s:iterator value="company.staffs" status="stat">
   Staff <s:property value="%{#stat.index}" /> 
      <s:property value="name" />  &nbsp;  <s:property value="age" />
      </br>

</s:iterator>

You can point to elements of Lists or Maps specifying their index with the IteratorStatus object: 您可以使用IteratorStatus对象指向Lists或Maps的元素,指定它们的索引:

<s:property value="company.name"/>
<s:property value="company.address"/>

<s:iterator value="company.staffs" status="stat">

    <span>Staff <s:property value="%{#stat.index}" /> info: </span>

    <label>name: </label>
    <s:textfield name="company.staffs[%{#stat.index}].name" />

    <label>age: </label>
    <s:textfield name="company.staffs[%{#stat.index}].age" />

    <br/>

</s:iterator>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM