Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Backed Form Bean

 
suvendu patra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic