You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by Nicholas Dierauf <ni...@autodesk.com> on 2011/08/15 09:38:20 UTC

Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Hello,

I am trying to populate form fields from an HttpServletRequest parameter map using Form.copyFrom(map). I am finding that all but HiddenField(s) are getting populated. For the HiddenField, an IllegalArguementException is getting thrown:

java.lang.IllegalArgumentException: The value.getClass(): '[Ljava.lang.String;' must be the same as the HiddenField valueClass: 'java.lang.String'
      at org.apache.click.control.HiddenField.setValueObject(HiddenField.java:243)
      at org.apache.click.util.ContainerUtils.copyMapToFields(ContainerUtils.java:1100)
...

The HiddenField is different from other fields in that it uses a 'valueObject' rather than a value String. I've tried setting the valueObject in the HiddenField constructor to match the String array that is used in the parameter map, but with no luck. I even tried creating my own HiddenField class, but the Form class still creates Click HiddenField(s) (for example for passing the form name), which throws the exception during copyFrom.

Does anybody have any suggestions/insights/work-arounds for this? I would love to leverage this useful method for populating form fields from a servlet request. I'm using Apache Click 2.3.0.

Thank you,
Nick.



RE: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Nicholas Dierauf <ni...@autodesk.com>.
Yeah. That's exactly what I have done. It is working fine now.
Thanks for your time, Bob.
Nick.

-----Original Message-----
From: Bob Schellink [mailto:sabob1@gmail.com] 
Sent: Monday, August 15, 2011 10:04 PM
To: user@click.apache.org
Subject: Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Ah I understand, however copying an array to a hidden or any other field won't work unless the field is customized to handle arrays.

What should work is to masage the request map into a new map that uses a String instead of array if there request map only has one entry. If it has multiple entries then leave it as an array. Should be easy to write a function that does this, eg:

Map mymap = MyUtil.convertToSingleValueMap(request);

regards

Bob

On 2011/08/15 19:43 PM, Nicholas Dierauf wrote:
> It does work fine with my custom HiddenField. It doesn't work with the Click HiddenField. The map returned from the Request.getParameterMap() method is a Map<String, String[]>  rather than a Map<String, String>. So the Click HiddenField.setValueObject(object) gets confused because it receives a String[] even when I have configured it to expect a valueClass of String[] (or String).
>
> If you were to create a simple Click Form named "myForm", then submit the form and attempt to populate a new Form object (server-side) using newForm.copyFrom(request.getParameterMap()), I believe that you will find that an IllegalArguementException is thrown when the Click HiddenField.setValueObject(stringArray) is called for this hidden field:
> <input id="myForm_form_name" type="hidden" value="myForm" 
> name="form_name">
>
> (This hidden field is populated automatically by Form.toString(), and 
> is a Click HiddenField object.)
>
> Thank you,
> Nick.
>
> -----Original Message-----
> From: Bob Schellink [mailto:sabob1@gmail.com]
> Sent: Monday, August 15, 2011 10:11 AM
> To: user@click.apache.org
> Subject: Re: Form copyFrom issue with HiddenField using 
> HttpServletRequest parameterMap
>
> On 2011/08/15 18:49 PM, Nicholas Dierauf wrote:
>> Hi Bob. I did override the get/setValueObject methods in my custom HiddenField class, which seemed to work for my own HiddenField members. But the Form class generates additional Click HiddenField members to pass a couple of extra parameters, including the form name. I don't have any control over which version of HiddenField that Click Form uses ...
>
> The copyForm iterates all fields and calls set/getValueObject on each one. I would expect that the HiddenFields Click added should still work fine even when mixed with your custom HiddenFields.
>
> regards
>
> Bob
>


Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Bob Schellink <sa...@gmail.com>.
Ah I understand, however copying an array to a hidden or any other field won't work unless the field 
is customized to handle arrays.

What should work is to masage the request map into a new map that uses a String instead of array if 
there request map only has one entry. If it has multiple entries then leave it as an array. Should 
be easy to write a function that does this, eg:

Map mymap = MyUtil.convertToSingleValueMap(request);

regards

Bob

On 2011/08/15 19:43 PM, Nicholas Dierauf wrote:
> It does work fine with my custom HiddenField. It doesn't work with the Click HiddenField. The map returned from the Request.getParameterMap() method is a Map<String, String[]>  rather than a Map<String, String>. So the Click HiddenField.setValueObject(object) gets confused because it receives a String[] even when I have configured it to expect a valueClass of String[] (or String).
>
> If you were to create a simple Click Form named "myForm", then submit the form and attempt to populate a new Form object (server-side) using newForm.copyFrom(request.getParameterMap()), I believe that you will find that an IllegalArguementException is thrown when the Click HiddenField.setValueObject(stringArray) is called for this hidden field:
> <input id="myForm_form_name" type="hidden" value="myForm" name="form_name">
>
> (This hidden field is populated automatically by Form.toString(), and is a Click HiddenField object.)
>
> Thank you,
> Nick.
>
> -----Original Message-----
> From: Bob Schellink [mailto:sabob1@gmail.com]
> Sent: Monday, August 15, 2011 10:11 AM
> To: user@click.apache.org
> Subject: Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap
>
> On 2011/08/15 18:49 PM, Nicholas Dierauf wrote:
>> Hi Bob. I did override the get/setValueObject methods in my custom HiddenField class, which seemed to work for my own HiddenField members. But the Form class generates additional Click HiddenField members to pass a couple of extra parameters, including the form name. I don't have any control over which version of HiddenField that Click Form uses ...
>
> The copyForm iterates all fields and calls set/getValueObject on each one. I would expect that the HiddenFields Click added should still work fine even when mixed with your custom HiddenFields.
>
> regards
>
> Bob
>


RE: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Nicholas Dierauf <ni...@autodesk.com>.
It does work fine with my custom HiddenField. It doesn't work with the Click HiddenField. The map returned from the Request.getParameterMap() method is a Map<String, String[]> rather than a Map<String, String>. So the Click HiddenField.setValueObject(object) gets confused because it receives a String[] even when I have configured it to expect a valueClass of String[] (or String). 

If you were to create a simple Click Form named "myForm", then submit the form and attempt to populate a new Form object (server-side) using newForm.copyFrom(request.getParameterMap()), I believe that you will find that an IllegalArguementException is thrown when the Click HiddenField.setValueObject(stringArray) is called for this hidden field: 
<input id="myForm_form_name" type="hidden" value="myForm" name="form_name">

(This hidden field is populated automatically by Form.toString(), and is a Click HiddenField object.)

Thank you,
Nick.

-----Original Message-----
From: Bob Schellink [mailto:sabob1@gmail.com] 
Sent: Monday, August 15, 2011 10:11 AM
To: user@click.apache.org
Subject: Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

On 2011/08/15 18:49 PM, Nicholas Dierauf wrote:
> Hi Bob. I did override the get/setValueObject methods in my custom HiddenField class, which seemed to work for my own HiddenField members. But the Form class generates additional Click HiddenField members to pass a couple of extra parameters, including the form name. I don't have any control over which version of HiddenField that Click Form uses ...

The copyForm iterates all fields and calls set/getValueObject on each one. I would expect that the HiddenFields Click added should still work fine even when mixed with your custom HiddenFields.

regards

Bob

Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Bob Schellink <sa...@gmail.com>.
On 2011/08/15 18:49 PM, Nicholas Dierauf wrote:
> Hi Bob. I did override the get/setValueObject methods in my custom HiddenField class, which seemed to work for my own HiddenField members. But the Form class generates additional Click HiddenField members to pass a couple of extra parameters, including the form name. I don't have any control over which version of HiddenField that Click Form uses ...

The copyForm iterates all fields and calls set/getValueObject on each one. I would expect that the 
HiddenFields Click added should still work fine even when mixed with your custom HiddenFields.

regards

Bob

RE: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Nicholas Dierauf <ni...@autodesk.com>.
Hi Bob. I did override the get/setValueObject methods in my custom HiddenField class, which seemed to work for my own HiddenField members. But the Form class generates additional Click HiddenField members to pass a couple of extra parameters, including the form name. I don't have any control over which version of HiddenField that Click Form uses ... unless I create my own Form class (which may uncover additional issues). 

Thanks Bob,
Nick.

-----Original Message-----
From: Bob Schellink [mailto:sabob1@gmail.com] 
Sent: Monday, August 15, 2011 7:33 AM
To: user@click.apache.org
Subject: Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Hi Nick,

HiddenField only works with non array values. When you create a custom hidden field make sure you override getValueObject/setValueObject to cast to an array.

regards

Bob

On Mon, Aug 15, 2011 at 2:32 PM, Gilberto <gi...@gmail.com> wrote:
> Hi, Nick!
>
> On Mon, Aug 15, 2011 at 4:38 AM, Nicholas Dierauf 
> <ni...@autodesk.com> wrote:
>> Hello,
>>
>>
>>
>> I am trying to populate form fields from an HttpServletRequest 
>> parameter map using Form.copyFrom(map). I am finding that all but 
>> HiddenField(s) are getting populated. For the HiddenField, an 
>> IllegalArguementException is getting thrown:
>>
>>
>>
>> java.lang.IllegalArgumentException: The value.getClass():
>> '[Ljava.lang.String;' must be the same as the HiddenField valueClass:
>> 'java.lang.String'
>>
>>       at
>> org.apache.click.control.HiddenField.setValueObject(HiddenField.java:
>> 243)
>>
>>       at
>> org.apache.click.util.ContainerUtils.copyMapToFields(ContainerUtils.j
>> ava:1100)
>>
>> ...
>>
>>
>>
>> The HiddenField is different from other fields in that it uses a 
>> 'valueObject' rather than a value String. I've tried setting the 
>> valueObject in the HiddenField constructor to match the String array 
>> that is used in the parameter map, but with no luck. I even tried 
>> creating my own HiddenField
>
>
> I did not understand your issue!
> Are you trying to set one HiddenField[1] with one array?
>
> Can you elaborate on it?
>
> Regards,
>
> Gilberto
>
> [1] 
> http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html
>

Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Bob Schellink <sa...@gmail.com>.
Hi Nick,

HiddenField only works with non array values. When you create a custom
hidden field make sure you override getValueObject/setValueObject to
cast to an array.

regards

Bob

On Mon, Aug 15, 2011 at 2:32 PM, Gilberto <gi...@gmail.com> wrote:
> Hi, Nick!
>
> On Mon, Aug 15, 2011 at 4:38 AM, Nicholas Dierauf
> <ni...@autodesk.com> wrote:
>> Hello,
>>
>>
>>
>> I am trying to populate form fields from an HttpServletRequest parameter map
>> using Form.copyFrom(map). I am finding that all but HiddenField(s) are
>> getting populated. For the HiddenField, an IllegalArguementException is
>> getting thrown:
>>
>>
>>
>> java.lang.IllegalArgumentException: The value.getClass():
>> '[Ljava.lang.String;' must be the same as the HiddenField valueClass:
>> 'java.lang.String'
>>
>>       at
>> org.apache.click.control.HiddenField.setValueObject(HiddenField.java:243)
>>
>>       at
>> org.apache.click.util.ContainerUtils.copyMapToFields(ContainerUtils.java:1100)
>>
>> ...
>>
>>
>>
>> The HiddenField is different from other fields in that it uses a
>> ‘valueObject’ rather than a value String. I’ve tried setting the valueObject
>> in the HiddenField constructor to match the String array that is used in the
>> parameter map, but with no luck. I even tried creating my own HiddenField
>
>
> I did not understand your issue!
> Are you trying to set one HiddenField[1] with one array?
>
> Can you elaborate on it?
>
> Regards,
>
> Gilberto
>
> [1] http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html
>

Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Gilberto <gi...@gmail.com>.
Hi, Nick!

On Mon, Aug 15, 2011 at 4:38 AM, Nicholas Dierauf
<ni...@autodesk.com> wrote:
> Hello,
>
>
>
> I am trying to populate form fields from an HttpServletRequest parameter map
> using Form.copyFrom(map). I am finding that all but HiddenField(s) are
> getting populated. For the HiddenField, an IllegalArguementException is
> getting thrown:
>
>
>
> java.lang.IllegalArgumentException: The value.getClass():
> '[Ljava.lang.String;' must be the same as the HiddenField valueClass:
> 'java.lang.String'
>
>       at
> org.apache.click.control.HiddenField.setValueObject(HiddenField.java:243)
>
>       at
> org.apache.click.util.ContainerUtils.copyMapToFields(ContainerUtils.java:1100)
>
> ...
>
>
>
> The HiddenField is different from other fields in that it uses a
> ‘valueObject’ rather than a value String. I’ve tried setting the valueObject
> in the HiddenField constructor to match the String array that is used in the
> parameter map, but with no luck. I even tried creating my own HiddenField


I did not understand your issue!
Are you trying to set one HiddenField[1] with one array?

Can you elaborate on it?

Regards,

Gilberto

[1] http://click.apache.org/docs/click-api/org/apache/click/control/HiddenField.html

RE: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Nicholas Dierauf <ni...@autodesk.com>.
Hi Malcolm. Thanks for your response.

Yeah, I'm aware that HttpServletRequest returns a Map<String, String[]> to accommodate multiple values with the same parameter name. In my case, I am just passing single values per parameter name, and Form.copyFrom(map) seems to handle all the Click Field types except for HiddenField. This is due to the overridden setValueObject() method defined in HiddenField. All of the other Click Field classes use the setValueObject() method defined in the abstract Field class, which seems to successfully handle copying the single value from the request map array.

I may be trying to do something beyond the original intent of Form.copyFrom(). As a work-around, I am thinking that perhaps I need to create a new Map based on the Request parameterMap, but with a single string value (rather than a String array), to feed to the copyFrom(map) method.

Best regards,
Nick.

-----Original Message-----
From: Malcolm Edgar [mailto:malcolm.edgar@gmail.com] 
Sent: Monday, August 15, 2011 4:01 AM
To: user@click.apache.org
Subject: Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

If you are doing something like this please note the method:

http://download.oracle.com/docs/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap()

This will return an array of Strings rather than a single string in the returned Map, is this because you can submit multiple values with the name parameter name in HTTP.

regards Malcolm Edgar

On Mon, Aug 15, 2011 at 5:38 PM, Nicholas Dierauf <ni...@autodesk.com> wrote:
> Hello,
>
>
>
> I am trying to populate form fields from an HttpServletRequest 
> parameter map using Form.copyFrom(map). I am finding that all but 
> HiddenField(s) are getting populated. For the HiddenField, an 
> IllegalArguementException is getting thrown:
>
>
>
> java.lang.IllegalArgumentException: The value.getClass():
> '[Ljava.lang.String;' must be the same as the HiddenField valueClass:
> 'java.lang.String'
>
>       at
> org.apache.click.control.HiddenField.setValueObject(HiddenField.java:2
> 43)
>
>       at
> org.apache.click.util.ContainerUtils.copyMapToFields(ContainerUtils.ja
> va:1100)
>
> ...
>
>
>
> The HiddenField is different from other fields in that it uses a 
> 'valueObject' rather than a value String. I've tried setting the 
> valueObject in the HiddenField constructor to match the String array 
> that is used in the parameter map, but with no luck. I even tried 
> creating my own HiddenField class, but the Form class still creates 
> Click HiddenField(s) (for example for passing the form name), which throws the exception during copyFrom.
>
>
>
> Does anybody have any suggestions/insights/work-arounds for this? I 
> would love to leverage this useful method for populating form fields 
> from a servlet request. I'm using Apache Click 2.3.0.
>
>
>
> Thank you,
>
> Nick.
>
>
>
>

Re: Form copyFrom issue with HiddenField using HttpServletRequest parameterMap

Posted by Malcolm Edgar <ma...@gmail.com>.
If you are doing something like this please note the method:

http://download.oracle.com/docs/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html#getParameterMap()

This will return an array of Strings rather than a single string in
the returned Map, is this because you can submit multiple values with
the name parameter name in HTTP.

regards Malcolm Edgar

On Mon, Aug 15, 2011 at 5:38 PM, Nicholas Dierauf
<ni...@autodesk.com> wrote:
> Hello,
>
>
>
> I am trying to populate form fields from an HttpServletRequest parameter map
> using Form.copyFrom(map). I am finding that all but HiddenField(s) are
> getting populated. For the HiddenField, an IllegalArguementException is
> getting thrown:
>
>
>
> java.lang.IllegalArgumentException: The value.getClass():
> '[Ljava.lang.String;' must be the same as the HiddenField valueClass:
> 'java.lang.String'
>
>       at
> org.apache.click.control.HiddenField.setValueObject(HiddenField.java:243)
>
>       at
> org.apache.click.util.ContainerUtils.copyMapToFields(ContainerUtils.java:1100)
>
> ...
>
>
>
> The HiddenField is different from other fields in that it uses a
> ‘valueObject’ rather than a value String. I’ve tried setting the valueObject
> in the HiddenField constructor to match the String array that is used in the
> parameter map, but with no luck. I even tried creating my own HiddenField
> class, but the Form class still creates Click HiddenField(s) (for example
> for passing the form name), which throws the exception during copyFrom.
>
>
>
> Does anybody have any suggestions/insights/work-arounds for this? I would
> love to leverage this useful method for populating form fields from a
> servlet request. I’m using Apache Click 2.3.0.
>
>
>
> Thank you,
>
> Nick.
>
>
>
>