You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ajay Patil <ap...@vertex.co.in> on 2003/07/09 07:04:16 UTC

Basic Code for Indexed Properties

>> I know this question would have been posted earlier also.
>> I want to know how to work with the indexed form fields and their 
>> property in the action form class.
>> Any link or ample code will be helpful.

>> Anurag Garg

Dear Anurag,

I have pasted the basic code that you need in the JSP, 
Form and Bean class to implement indexed properties.

This will show a page with dynamic number of textboxes and
For each textbox, it will get/set the particular property
within your collection of beans that is inside your form.

HTH,
Ajay

JSP Page
--------

<html:form name="TestForm" type="test.TestForm" action="/test">
<logic:iterate name="TestForm" 
       property="indexedBeans" id="indexedBeans"
    <!-- Please note that id in logic:iterate tag should match
         the name in html:text tag -->
    <html:text name="indexedBeans" property="prop1" indexed="true" />
    <html:text name="indexedBeans" property="prop2" indexed="true" />
</logic:iterate>
</html:form>

Form Class
----------

public ArrayList getIndexedBeans() {
  return this.myBeans;
}

// THIS METHOD IS REQUIRED IF YOUR FORM IS OF REQUEST SCOPE.
public MyBean getIndexedBeans(int index) {

  while (this.myBeans.size() < index) 
    this.myBeans.add(new MyBean());

  return (MyBean) this.myBeans.get(index);
}

Bean Class
----------

public class MyBean {
  private String prop1;
  private String prop2;
  
  public String getProp1() { 
   return prop1; 
  }
  
  public void setProp1(String prop1) {
   this.prop1 = prop1:
  }

  public String getProp1() {
   return this.prop2;
  }

  public void   setProp2(String prop2) {
    this.prop2 = prop2;
  }
}

----------------------------------------------------
Ajay Patil
Vertex Software Pvt. Ltd.
apatil@vertex.co.in
http://www.vertex.co.in
----------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org