html:select of struts

Hi all,

This is what i am doing in the JSP page,

<html:select property="responsibleUnitsId" multiple="true">
<html:options collection="responsibleUnitOptionsList" labelProperty="name" property="responsibleUnitId"/>
</html:select>



Inside the form i have the following,

private String[] responsibleUnitsId;

public String[] getResponsibleUnitsId() {
            return responsibleUnitsId;
      }

public void setResponsibleUnitsId(String[] responsibleUnitsId) {
            this.responsibleUnitsId = responsibleUnitsId;
      }


In the Action Handler this is what i am doing.... .

int size = request.getParameter("responsibleUnitsId").length();
System.out.println("XXXXXXXXX  Size of responsible Units list is   " + size);
responsibleUnitsId=new String[size];
System.arraycopy(mlForm.getResponsibleUnitsId(),0,responsibleUnitsId,0,size);
//responsibleUnitsId = request.getParameterValues("responsibleUnitsId");
java.util.Set responsibleUnits = new HashSet();
for(int i=0;i<size;i++)
{
      ResponsibleUnit responsibleUnit=(ResponsibleUnit)m3Manager.getResponsibleUnit(responsibleUnitsId[i]);
      responsibleUnits.add(responsibleUnit);
}
mlForm.setResponsibleUnits(responsibleUnits);

ResponsibleUnits is of type java.util.Set. (FYI)

Now, the problem is the size of the request parameter is always 1, no matter how many units i select on the JSP page.

Any suggestion,

Thanks.
letsbedecentAsked:
Who is Participating?
 
aozarovCommented:
>> Do we need to have a property in action form for every field in the html form ??
No, that is only a conveient way to access request data which is supported by struts.

>> cant we just use request.getParameter() when we dont want a property in the action
Sure, you can though probably you want to always use the plural style (and in some cases to get an array of one item).

>> but why would it complain when i was using only request.getParameter()
this returns a string. A string has a method length();

>> my previous example: int size = request.getParameterValues("responsibleUnitsId").length();
It is actually should be int size = request.getParameterValues("responsibleUnitsId").length; // after all it returns an array
0
 
aozarovCommented:
>> int size = request.getParameter("responsibleUnitsId").length();
From the Javadoc:
You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.lang.String).

So change it to:
int size = request.getParameterValues("responsibleUnitsId").length();
or to
int size = rmlForm.getResponsibleUnitsId().length();
0
 
letsbedecentAuthor Commented:
ooooops, i totally forgot that...

yeah, but why would it complain when i was using only request.getParameter() and was not having corresponding property in the action form

Do we need to have a property in action form for every field in the html form ?? cant we just use request.getParameter() when we dont want a property in the action form for this field.. ??
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.