tags:

views:

268

answers:

2

Hello,

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.

A: 

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 calling ValueStack#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.

leonbloy
if I say bean, then by the Java Beans standards getters and setters are incorporated without the need to say. I just tried to be clearer.Further, as you say nothing favourable is there, I shall go for ModelDriven. :)
hkansal
A: 

ModelDriven was the correct choice :)

hkansal