Chailie
Chailie

Reputation: 451

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:

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:

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

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,e.g maybe i will define "form" as following

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

Upvotes: 1

Views: 2100

Answers (3)

Andrea Ligios
Andrea Ligios

Reputation: 50203

You can point to elements of Lists or Maps specifying their index with the IteratorStatus object:

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

Upvotes: 1

Deepak
Deepak

Reputation: 327

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

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

Upvotes: 1

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

You can use Struts2 Iterator tag to iterator over the list and show all information on your JSP page

Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator

For more details refer to Iterator tag example.

Upvotes: 1

Related Questions