This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes Map Backed Form Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Map Backed Form Bean" Watch "Map Backed Form Bean" New topic
Author

Map Backed Form Bean

suvendu patra
Greenhorn

Joined: Nov 19, 2002
Posts: 5
hi,
i am trying to create dynamic Checkboxes in jsp using hashmap, for which i am using mapbacked actionform.
=======================
my form bean class contain these two getter & setter methods:
public String getMap(String strKey)
{
return map.get(strKey).toString();
}
public void setMap(String strKey, String strValue)
{
map.put(strKey, strValue);
}
=======================
jsp file contain this for populating the dynamic
checkbox. where map is a HashMap.
for(int i = 0; i < map.size(); i++)
{
String strName = "map(map-" + i + ")";
<html:checkbox property="<%=strName%>"/>
}
=======================
while executing am getting an errror :
"javax.servlet.jsp.JspException: No getter method for property map(map-0) of bean org.apache.struts.taglib.html.BEAN"
=====================================
can anyone get back to me on how to solve it.
suvendu
Michael Sorouni
Greenhorn

Joined: Dec 01, 2004
Posts: 2
I've noticed that there's alot of questions regarding using the checkbox tag with a map-backed ActionForm with not alot of answers(actually none), and since I had a similar problem at hand I had no other chose but to solve it myself.
Driving home from work yesterday it suddenly struck me after realising that since the underlying property value associated with the checkbox field should be of type boolean then it would seem erroneous to use a setter method in the form that accepted a parameter of type Object as is suggested in the Struts User Guide.

i.e public void setXXX(String key, Object value)

Instead, it seemed to me plausible that the struts framework could be looking for a setter method that contained a parameter of type boolean, which is the reason an InvocationTargetException was thrown.

I tested this idea the first thing this morning and lo and behold the ActionForm's method that I was expecting to be called was indeed called.

So for all of you who have spend days (or maybe weeks) baffling over this problem the solution is to use a method signature of the following nature

public void setXXX(String key, boolean value) where XXX is any name

Good luck.
Michael Sorouni
Greenhorn

Joined: Dec 01, 2004
Posts: 2
The other problem of course as the apache web site states is that a checkbox that is unchecked is never included in the request and the funny thing is that the struts framework doesn't somehow intercept the call and add it to the request. So what happens? Well if the checkbox is unchecked and then you check it, when the form is submitted, the method in the ActionForm specified in the property attribute of the html:text tag is called. However, the reverse is not true. If the checkbox is checked and then you uncheck it, struts doesn't call the appropriate method in the ActionForm. Bummer!
So what can you do? Well you do what the struts people recommend i.e to manually ascertain that the element doesn't exist in the request therefore the corresponding value is unchecked. This could of course be done by you in the reset method (so that makes me think why it is not automatically done for us).
It didn't take me too long to do, but it seemed such a primitive aspect of struts, and should be seriously considered for improvement.

Good luck
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map Backed Form Bean