1

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

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes,That's exactly what i expected,Thanks,By the way,Can you recommend any tutorial of how to use OGNL in such case?e.g in List,Map or any other commonly used java data structure
@Chailie On the wiki there's some examples, but they are scarce ;)
Sorry @Chailie , no. But they're always the same... [%{#stat.index}] for numeric indexes (Lists), ['%{#stat.index}'] for String keys (Maps). Feel free to dig into my OGNL and Struts2 tagged question (My profile -> tags -> click on the tag -> browse the answers), maybe you'll find something interesting about this :)
1

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.

Comments

1

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>

2 Comments

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"?
Yes you can do this by using <s:textfield name="" /> tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.