Struts2 form to action fields mapping automatically
I would like to know if it开发者_运维技巧 is possible, in Struts2, to map an HTML form's fields to those of an action, automatically, without getters and setters.
It is clear that by getters and setters or the ParameterAware interface and the Map
, fields can be set in the action, but I just wanted to know if otherwise there was a way.
First, instead of thinking in terms of "with fields with getters and setters" you are advised to think in terms of "bean properties" here. Struts2 (and most java frameworks) think in that way, they usually don't care (and rightly so) whether those "properties" are real fields or not.
The short answer to your question is: no.
But be aware that Struts2 is very flexible - when I say "no" I mean "using the default interceptors". You could always write your own interceptor instead of the default to do that - bad idea IMO.
The interceptor that does that mapping is (basically) the parameters interceptor. From its documentation:
This interceptor gets all parameters from
ActionContext#getParameters()
and sets them on the value stack by callingValueStack#setValue(String, Object)
typically resulting in the values submitted in a form request being applied to an action in the value stack.
And looking into ValueStack.setValue(String,Object) we read:
Attempts to set a property on a bean in the stack with the given expression using the default search order.
So there you have.
ModelDriven was the correct choice :)
精彩评论