Multiple field ognl type converter
I have a form with (at the moment) two fields and submit the following:
capture.id = 213
capture.description = DescriptionText
The target object 'capture' is immutable and I would like to provide a type converter to take both values and call the constructor. What I cannot seem to do is get by TypeConverter to be invoked.
If the input is simply:
capture = foo
Then the type converter is called, but obviously this isn't much use, is there away to make a ognl delegate the rest of the rest of the type conversation to me, perhaps passing in a Map of the parameters?
Any ideas? Is this even possible in struts2
versions: struts 2.0.14 & ognl 2.6.11
EDIT: I've done a bit of reading on this and my next attempt seemed to me to be a good plan. My theory was that using the Map syntax would make Ognl convert the values to a map and then call my converter with that map to convert it to my value.
capture[id] = 213
capture[description]开发者_StackOverflow中文版 = DescriptionText
Nope that doesn't seem make any difference at all.
The way I did this was to have the following in the JSP:
<s:textfield name="capture" value="capture.id" />
<s:textfield name="capture" value="capture.description" />
In the type converter, the String[] values parameter of the convertFromString method will contain both values needed to construct a new immutable capture. Provided that you are consistent with the text field ordering (or better yet, encapsulate it in a tag file), you can use the indexes of the values array to reliably get the appropriate field of the capture object.
The one weird part about this approach is that the convertToString method doesn't really do anything for you. You can return either id or description (or concatenate them together), but since you are using the values attribute in the JSP, it doesn't matter.
It seems the that the answer is no you can't do that with struts2.
I've posted this question on the struts2 mailing list and it seems that it just isn't possible to have multiple fields be presented to a TypeConverter
.
The alternative solution suggested is to have mutable object with setters and then have some form of 'petify' method to prevent any future changes.
For my project I've actually implemented another struts Interceptor to implement my custom parameter binding behaviour.
精彩评论