PDA

View Full Version : form:hidden path on String array


thehl
Jul 12th, 2007, 07:53 AM
I wish to have a hidden form field, such: <form:hidden path="answer"/>.

The issue is that getAnswer returns a String array. How do I put this answer into element [0]?

thanks!

pmularien
Jul 12th, 2007, 12:02 PM
<form:hidden path="answer[0]"/>
... should work. Just make sure you populate it with a value in formBackingObject if it's not already present, to avoid the dreaded nested null exception :)

thehl
Jul 18th, 2007, 12:53 PM
This does not seem to work. I get the following error:

22405 ERROR org.apache.struts.taglib.tiles.InsertTag | ServletException in '/WEB-INF/view/question.jsp': Invalid property 'answer[0]' of bean class [xxx.
eJuror.web.forms.SimpleQuestionCommand]: Bean property 'answer[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.apache.jasper.JasperException: Exception in JSP: /WEB-INF/view/question.jsp:176

173: <spring:message code="${kvp.value}" htmlEsca
pe="false"/>
174: </TD>
175: <TD align="left">
176: <form:radiobutton path="answer[${ix}]"
177: value="${kvp.key}"
178: htmlEscape="false"
179: onchange="getButton().disabled
=false;"

Any other thoughts?

thehl
Jul 19th, 2007, 08:21 AM
I think this is a bug in the spring framework stuff. Have a look.

Here's the first part of my stack trace:

60922 ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/ejur
or].[jsp] | Servlet.service() for servlet jsp threw exception
org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'answer
[0]' of bean class [com.acs.gs.eJuror.web.forms.SimpleQuestionCommand]: Bean pro
perty 'answer[0]' is not readable or has an invalid getter method: Does the retu
rn type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPrope rtyValue(BeanWrappe
rImpl.java:534)
at org.springframework.beans.BeanWrapperImpl.getPrope rtyValue(BeanWrappe
rImpl.java:526)
at org.springframework.web.servlet.support.BindStatus .<init>(BindStatus.
java:144)

So looking at line 534 (Spring 5.05) of BeanWrapperImpl.java, we find:

if (pd == null || pd.getReadMethod() == null) {
throw new NotReadablePropertyException(getRootClass(), this.nestedPath + propertyName);
}

So this is probably where it's going through reflections to find a property getAnswer[0]() which doesn't work b/c it is getAnswer(), so I'm thinking Spring doesn't work with arrays at all, which would be disappointing.

Until I look a few lines down where it retrieves it:

else if (value.getClass().isArray()) {
value = Array.get(value, Integer.parseInt(key));
}


This definitely appears to be handling an array based on the thing with the keys from the PropertyTokenHolder, which, it turns out is created just above the call to this method on line 526:

PropertyTokenHolder tokens = getPropertyNameTokens(getFinalPath(nestedBw, propertyName));
return nestedBw.getPropertyValue(tokens);


So I looked up getPropertyNameTokens() and it appears to find the array parameters and handle them appropriately.

It looks like, then, that this is all set up to handle arrays except that it never trims the array notation ([0]) from the property name before trying to find it through reflection. Therefore, this appears to be a bug in version 5.05.

Please comment before I open a JIRA.

Thanks.

thehl
Jul 19th, 2007, 08:38 AM
Sorry, gang. The name of the property was answers and not answer. This caused the problem.

Sorry to waste your time.:rolleyes: