struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Hardy <ahardy.str...@cyberspaceroad.com>
Subject Re: [S2] Writing a simple interceptor that set a properties of the action.
Date Thu, 06 Mar 2008 12:47:49 GMT
Unless you want to do it via introspection, then a good way would be to set the 
appropriate actions to implement an interface having that setter.

Then you can use instanceof to see if the action in your interceptor implements 
your interface. If so, cast it to the interface and call the setter.

I think this is generally the way that many S2 interceptors work. For instance, 
look at ValidationAware in ParametersInterceptor.

HTH
Adam

GF on 06/03/08 12:06, wrote:
> I want to do a simple thing,
> I have an action like this
> 
> public class MyAction {
> private String myVar;
> 
> public setMyVar(String myVar) {
> this.myVar = myVar;
> }
> }
> 
> Inside my Interceptor i want to put in my Action a string inside myVar
> property of the Action.
> In few words, I need to discover if the Action has that setter, and if
> its parameter type is a "String", in this case I would call that
> setter.
> 
> I know how to write the interceptor but i don't know how to "try" to
> set a property on the action.
> 
> Can you give me any good link?
> I tried to watch the source of : StaticParametersInterceptor  that
> should do something like I have to do.
> 
> It does:
>         // for actions marked as Parameterizable, pass the static
> parameters directly
>         if (action instanceof Parameterizable) {
>             ((Parameterizable) action).setParams(parameters);
>         }
> 
>         if (parameters != null) {
>             final ValueStack stack = ActionContext.getContext().getValueStack();
> 
>             for (Iterator iterator = parameters.entrySet().iterator();
>                  iterator.hasNext();) {
>                 Map.Entry entry = (Map.Entry) iterator.next();
>                 stack.setValue(entry.getKey().toString(), entry.getValue());
>                 Object val = entry.getValue();
>                 if (parse && val instanceof String) {
>                     val = TextParseUtil.translateVariables((String) val, stack);
>                 }
>                 stack.setValue(entry.getKey().toString(), val);
>             }
>         }
> 
> Where does it call the "action setters"?
> Thanks
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message