You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sashi Ravipati <Ra...@michigan.gov> on 2003/06/12 20:06:16 UTC

RE: what setters do i implement in an indexedtag--NewBiequestion

John

I had a question on how to do the prep action.

[This assumes the prep action that entered the jsp figured out the size
of the list and set it into a hidden in the jsp in a field named
personListLength.]

I tried like this but not sure if this is right

In my action  i added these lines

ArrayList personList=new ArrayList();
      personList.add(new PersonBean());
      request.setAttribute("personList",personList);
       return mapping.findForward("success");

I tried to retrieve it in the JSP page but did not get the value. I must
be doing something wrong.

How do I set the value for the hidden  variable in the JSP page u
suggested. 

>>> jgreenhill@dayspring-tech.com 06/12/03 01:54PM >>>
Hi,

To start with, I'm assuming a flow like this:

    prepAction --> jsp --> saveAction

Now if you comment the hard-code 5 and un-comment getting the length
from a hidden, then the prepAction determines the length of the list
(which can vary) and sets the length into the jsp.

I believe you're saying that the first time in a user sees 5 rows, but
now wants to add (for example) 1 more and (I assume) fill it with data.

Off the top of my head, maybe you could add an "Add Row" button that
submitted the form. The saveAction detected this wasn't really a save
but just adding a row, so it takes the personList, adds a blank
PersonForm to the end, changes the length field to the correct size and
returns back to the jsp.

When the "Save" button is clicked, the saveAction logic skips the add
row stuff and does the save, then forwards to wherever you want to go...

The iterate tag doesn't care how many there are, it always goes through
the whole list. The length is only required, as you know, to ensure we
have PersonBeans in the list to populate.

That's my first idea, there are probably better ones out there.

-john


-----Original Message-----
From: Sashi Ravipati [mailto:RavipatiS@michigan.gov]
Sent: Thursday, June 12, 2003 8:08 AM
To: struts-user@jakarta.apache.org
Subject: RE: what setters do i implement in an
indexedtag--NewBiequestion


John

Made few changes and code worked fine. 

Would like to extend this so that it will work for handling  Dynamic
table situation-- Let me explain

If u see in Action form reset method I initialized it to 5 (num="5 "for
testing) so the page loads with five empty rows, But we have a
requirement where the user can add more rows to the table and click
submit.

1. Action form code:

private ArrayList personList;
    
  public void reset(ActionMapping mapping, HttpServletRequest request){
     this.personList = new ArrayList();
   // String num = request.getParameter("personListLength");
     String num ="5";
        try {
            if (num != null) {
                int len = Integer.parseInt(num);
                for (int i = 0; i < len; i++)
                    this.personList.add(new PersonBean());
            }
        }catch (NumberFormatException e) {
           e.printStackTrace();
        }
    }
  public PersonBean getPerson(int ndx) {
        return (PersonBean) personList.get(ndx);
    }
  public void setPersonList(int ndx, PersonBean p) {
        personList.set(ndx, p);
    }
  public ArrayList getPersonList(){
    return this.personList;
  }

2. jsp

<TABLE id="mytable">
  <logic:present name="personList"> 
<logic:iterate id="person" name="dynaTableForm" property="personList" 
   type="com.learning.struts.PersonBean" >

  <tr>
    <td>
       <html:text name="person" property="firstName" indexed="true"/>
    </td>
    <td>
        <html:text name="person" property="phoneNo" indexed="true"/>
    </td>
  </tr>
</logic:iterate>
</logic:present>
  
</TABLE>

3.PersonBean:
public class PersonBean implements Serializable
{
  String firstName;
  String phoneNo;

  public PersonBean()
  {
    super();
  }

  public String getFirstName()
  {
    return firstName;
  }

  public void setFirstName(String newFirstName)
  {
    firstName = newFirstName;
  }

  public String getPhoneNo()
  {
    return phoneNo;
  }

  public void setPhoneNo(String newPhoneNo)
  {
    phoneNo = newPhoneNo;
  }
}

Let me know if u need any further info.

Thanks


>>> RavipatiS@michigan.gov 06/12/03 10:56AM >>>
John

Made few changes and code worked fine. 

Would like to extend this so that it will work for handling  Dynamic
table situation-- Let me explain

If u see in Action form reset method I initialized it to 5 (num="5 "for
testing) so the page loads with five empty rows, But we have a
requirement where the user can add more rows to the table and click
submit.

Action form code:

private ArrayList personList;
    
  public void reset(ActionMapping mapping, HttpServletRequest request){
     this.personList = new ArrayList();
   // String num = request.getParameter("personListLength");
     String num ="5";
        try {
            if (num != null) {
                int len = Integer.parseInt(num);
                for (int i = 0; i < len; i++)
                    this.personList.add(new PersonBean());
            }
        }catch (NumberFormatException e) {
           e.printStackTrace();
        }
    }

.jsp

<TABLE id="mytable">
  <logic:present name="personList"> 
<logic:iterate id="person" name="dynaTableForm" property="personList" 
   type="com.learning.struts.PersonBean" >

  <tr>
    <td>
       <html:text name="person" property="firstName" indexed="true"/>
    </td>
    <td>
        <html:text name="person" property="phoneNo" indexed="true"/>
    </td>
  </tr>
</logic:iterate>
</logic:present>
  
</TABLE>

  
  public PersonBean getPerson(int ndx) {
        return (PersonBean) personList.get(ndx);
    }
  public void setPersonList(int ndx, PersonBean p) {
        personList.set(ndx, p);
    }
  public ArrayList getPersonList(){
    return this.personList;
  }

PersonBean:
public class PersonBean implements Serializable
{
  String firstName;
  String phoneNo;

  public PersonBean()
  {
    super();
  }

  public String getFirstName()
  {
    return firstName;
  }

  public void setFirstName(String newFirstName)
  {
    firstName = newFirstName;
  }

  public String getPhoneNo()
  {
    return phoneNo;
  }

  public void setPhoneNo(String newPhoneNo)
  {
    phoneNo = newPhoneNo;
  }
}

Let me know if u need any further info.

Thanks




>>> jgreenhill@dayspring-tech.com 06/11/03 06:45PM >>>
Forgot to mention, I believe for the case of indexing using
logic:iterate your id must match your property. So in your example set
id="personList"

Now if you switch over to nested:iterate, the whole id thing goes away
and the property and type attributes are sufficient.

-john

-----Original Message-----
From: John Greenhill [mailto:jgreenhill@dayspring-tech.com]
Sent: Wednesday, June 11, 2003 3:37 PM
To: Struts Users Mailing List
Subject: RE: what setters do i implement in an indexed tag
--NewBiequestion


Assuming you're NOT using the lazy initialization, you could do it like
this...



1) your iterate will need to specify the type of the nested class:

<logic:iterate id="person" name="myFormBean" property="personList"
type="com.mydom.myproject.struts.Person>

Where the Person class has just the standard accessors for name and
phoneNo.


2) Your myFormBean will have the special accessors for indexing:

    // SPECIAL ACCESSORS FOR INDEXING NEEDED
    public Person getPersonList(int ndx) {
        return (PersonForm) personList.get(ndx);
    }
    public void setPersonList(int ndx, PersonForm p) {
        personList.set(ndx, p);
    }


3) Assuming you don't already know the size of your personList, your
reset method
will initialize it and create the empty Person objects to fill with the
data:

    public void reset(ActionMapping mapping, HttpServletRequest request)
{

        this.personList = new ArrayList();

        String num = request.getParameter("personListLength");
        try {
            if (num != null) {
                int len = Integer.parseInt(num);
                for (int i = 0; i < len; i++)
                    this.personList.add(new PersonForm());
            }
        } catch (NumberFormatException e) {
            //...
        }
    }

This assumes the prep action that entered the jsp figured out the size
of the list
and set it into a hidden in the jsp in a field named personListLength.

-john

-----Original Message-----
From: Sashi Ravipati [mailto:RavipatiS@michigan.gov]
Sent: Wednesday, June 11, 2003 1:19 PM
To: struts-user@jakarta.apache.org
Subject: what setters do i implement in an indexed tag --NewBiequestion


Can some body help in as how to define get and set methods when we use
indexed tags.

eg:

<logic:iterate id="person" name="myFormBean" property="personList">
  <tr>
    <td>
      <bean:write name="person" property="name"/>
      <html:hidden name="person" property="name" indexed="true"/>
    </td>
    <td>
      <html:text name="person" property="phoneNo" indexed="true"/>
    </td>
  </tr>
</logic:iterate>

What will be the get and set methods. 

Thanks

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

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