PDA

View Full Version : Usage of marker parameters feature



arsenjew
Aug 17th, 2004, 07:41 AM
I just tried the new field marker prefix feature of the ServletRequestDataBinder (automatically supplementing request parameters for an unchecked checkbox), and it worked quite well. My solution doesn't seem too smart to me, though:
I set the prefix in the initBinder method of my controller:

binder.setFieldMarkerPrefix("checkbox_");
and added a hidden parameter to my form:

<input type="hidden" name="checkbox_<jstl&#58;out value="$&#123;status.expression&#125;"/>" value="irrelevant"/>
Hardcoding the prefix like this is awkward. One possible solution would be to add some sort of global config bean to my application context and retrieve the prefix like this:

WebApplicationContext context = &#40;WebApplicationContext&#41;request.getAttribute&#40;"org.springframework.web.servlet.DispatcherServlet. CONTEXT"&#41;;
GlobalConfigBean config = &#40;GlobalConfigBean&#41;context.getBean&#40;"config"&#41;;
binder.setFieldMarkerPrefix&#40;config.getFormPrefix&#40;&#41; &#41;;

... and in my jsp:


<input type="hidden" name="<jstl&#58;out value="$&#123;config.formPrefix&#125;"/><jstl&#58;out value="$&#123;status.expression&#125;"/>" value="irrelevant"/>

Is there any "official" recommendation on how to use the field marker prefix in a more elegant way?

Kind regards

Alef Arendsen
Aug 17th, 2004, 09:00 AM
Is there any "official" recommendation on how to use the field marker prefix in a more elegant way?


Maybe we should add a fieldMarker property to the BindStatus object, allowing you to do the following



<input type="hidden" name="<c&#58;out value="$&#123;status.fieldMarker&#125;"/>"/>

arsenjew
Aug 17th, 2004, 09:26 AM
You do not suggest to amend the framework, do you? That my first posting to this forum should have such an impact rather scares me ;-)
Yet another question concerning the field marker:
I cannot use it to automatically reset the values of an array of properties (say, for a row of checkboxes or a multiple select list), can I? At least I can see no way how the binder should know what length to initialize the array with.

Alef Arendsen
Aug 18th, 2004, 01:51 AM
Well why not ;-).

I think we're a bit too late here, we're almost releasing a 1.1 final and I don't think it'll make it, but I'll suggest it at the dev list.


About the array? No, not really, but you could always do that programmatically (although this is a hassle of course) using one of the on**** methods (can't remember which one is called before validation, you have to check the JavaDOC for that).

alef

arsenjew
Aug 18th, 2004, 07:58 AM
(Just for the records):
Using the field marker technique with an array of properties will leave you with an array of length 0 if the request contained no parameter value for the property in question.
In order to re-initialize your array you can override onBindAndValidate in your Controller, simply copying the parameter value array to your property array:



String&#91;&#93; myProp = new String&#91;&#93;;
if &#40;request.getParameter&#40;"myProp"&#41;!=null&#41;&#123;
myProp = &#40;String&#91;&#93;&#41;request.getParameterValues&#40;"myProp"&#41;.clone&#40;&#41;;
&#40;&#40;myCommand&#41;command&#41;.setMyProp&#40;myProp&#41;;
&#125;


(Overriding onBindAndValidate would have been the way to deal with this anyway before the introduction of the field marker prefix, cf. http://opensource.atlassian.com/projects/spring/browse/SPR-111)