andy wix wrote:

session). To get the ID would be something, but it would save me needing an action class at all if I could get the contact object representing the 'clicked' row into the session or request scope.

To do what you want above would take a slightly different approach. Say you had your ArrayList of Contacts in Session scope and after the user clicked on a link you wanted to edit one of those objects. In this case you wouldn't use the "ID" to look him up you'd have to use the index of where you were in the Collection so that when you submit to the Action you can then pull the user out like so...


//on JSP in a loop of you contacts
<c:forEach items="${contacts}" var="contact" varStatus="status">

<c:url var="url" value="/do/modifyContact">
 <c:param name="indexPos" value="${status.index}"/>
</c:url>
<a href="<c:out value='${url}'/>">Modify</a>

//in Action...

//from parameter on the JSP
indexPos = new Integer(request.getParamater("indexPos").intVal();

//grab your existing ArrayList
ArrayList contacts = (ArrayList)request.getSession().getAttribute("contacts");


//now pull out the one you want and probably set it in request scope
//for the next update page to use
Contact contact = (Contact)contacts.get( indexPos );

request.setAttribute( "contact", contact );
//forward to update JSP page

The above being said, that 'usually' isn't the approach you want to take (although it's not always bad). The reason is A) you are required to stuff possibly a large Collection into Session scope and B) Each object in the Collection needs to be fully populated, which could be expensive if each item had large objects nested inside, etc. The more typical approach we were describing would involve a call to populate the Contact based on a unique indentifier such as an 'id'

Imagine a case where you just wanted the user to choose a contact to update.. you'd probably just be displaying their name and maybe a couple other attributes (but probably not all the other contact information - alternate phone numbers etc). So really all you need for this first list is a simple Collection put in request scope of Contact beans with just a few things populated. Then the user clicks on the row and you can then fully populate the Contact. (This also has the added benefit of helping you have a better chance of getting a fresh unmodified Contact). Using the first approach, what if the user was looking at the list for a while and went to the bathroom and came back 15 minutes later and went to update the contact. You would end up giving him a Contact from teh Collection that could have been modified (or even deleted) by another user. On the other hand, if you get a fresh population of the Contact you can alert the User the Contact was deleted or he'd at least see more recently modified changes.

And in the below you woldn't need the form tag (like you mention). However you would need to be getting the list from somewhere so this part is probably wrong: value="${form0.tempContact.id}"/>

Hope that helps.

A cut down version of the code is:

<form name="form0" method="post">
<TABLE border="0">
     <%
     ArrayList list = (ArrayList)session.getAttribute("Contacts");
     Iterator it = list.iterator();
     int id = 0;
     while(it.hasNext()) {
        Contact tempContact = (Contact)it.next();
      }%>
    <TR>
       <TD><%=tempContact.getName()%></TD>
       <TD><%=tempContact.getNumber()%></TD>

       <TD><c:url var="url" scope="session" value="/do/modifyContact">
               <c:param name="ID" value="${form0.tempContact.id}"/></c:url>
               <a href="<c:out value='${url}'/>">Modify</a>

Note that I am not using any Struts Forms for this Action as I receive the ArrayList from the session. I added an html form tag as above because you had one in the example code.



--
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to