aspose file tools*
The moose likes Struts and the fly likes Set Bean from JSP using Index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Set Bean from JSP using Index" Watch "Set Bean from JSP using Index" New topic
Author

Set Bean from JSP using Index

RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
Hi all,

I am using JDK 1.4, Struts 1.2.9, WAS 6.0, Win XP.
Page1.jsp- Has a link to show the ordering time and other information for the purchased items for a particular user.
Page2.jsp- Opens the jsp and does a <bean:write> from a collection-ArrayList(which has Java Bean Objects).
ResultBean.java - This Java bean Object has getXXX() and setXXX() for all the information that is displayed on the Page2.jsp.

The problem is that I want to write the "datesubmittedfrom" (using a calender) from the Page2.jsp. It should be an editable field whereas the others are read only. This field should be set in the ResultBean.java( the bean and not the Action Form for the same JSP)
I am confused, the UserBean.java is populated from the database(after some manipulation). Can i call the setDatesubmittedfrom()directly from the JSP ??
I have used indexed properties and saw a recent post also given by Merrill, but still couldnt reach a solution.
If this is not the right way, can you please guide me through the correct way.
Help would be appreciated.
Thanks,
Roshani

------------------------
CalValueForm.java (ActionForm)

public class CalValueForm extends ActionForm
{
private ResultBean allData=null;
private ArrayList results;

public ResultBean getAllData(int index){
return (ResultBean) this.results.get(index);

}

public void setAllData(int index, ResultBean resultBean){
this.results.set(index,resultBean);
}
}
----------------
ResultBean.java (Java Bean Object)

public class ResultBean
{
private static StringNOT_A_DATE= "mm/dd/yy";
private Stringdatesubmittedfrom= NOT_A_DATE;
// other getXXX() and setXXX()

public void setDatesubmittedfrom(String datesubmittedfrom) {
this.datesubmittedfrom = datesubmittedfrom;
}

public String getDatesubmittedfrom() {
return datesubmittedfrom;
}
}
--------------------
Page2.jsp

<logic:iterate id="result" name="inputForm" property="results" indexId="ctr">

<tr>
<td><bean:write name="result" property="itemID"/></td>
<td><bean:write name="result" property="typeOfItem"/></td>
<td><bean:write name="result" property="PurchaseDate"/></td>
<td>
<html:form action="/addCal" styleId="calValueForm">
<html:text name="calValueForm"
property= '<%= "allData[" + ctr + "].datesubmittedfrom" %>' />
</html:form>
</td>
</tr>

</logic:iterate>
------------------------

[ October 11, 2006: Message edited by: RoshaniG Gopal ]

[ October 11, 2006: Message edited by: RoshaniG Gopal ]
[ October 11, 2006: Message edited by: RoshaniG Gopal ]

Regards,<br />Roshani
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Everything looks good except for one thing:

The <html:form> tag should not be inside the <logic:iterate> loop. The beginning tag should be right after the <body> tag and the end tag should be right before the </body> tag, so that there is only one form on the page.

If your ActionForm is in session scope, it should work fine. However, if your ActionForm is in request scope, you will also have to add some "lazy initialization" behavior to your indexed getter. This link will explain how to do it.


Merrill
Consultant, Sima Solutions
RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
Hi Merrill,
Thanks for the reply. But i was not able to set the value of setDatesubmittedfrom() in the ResultBean. I tried after removing the <html:form> as you suggested.

Regards,
Roshani
 
Don't get me started about those stupid light bulbs.
 
subject: Set Bean from JSP using Index
 
Similar Threads
using indexId
Trouble with indexed properties
How to use checkbox in iterate tag ?
Checkboxes in iterate loop
why+beans+are+used?