You should be able to create an interface for your actions like:
public interface IUserAware {
Long getUserId();
Void setUserId(Long userId);
}
In your interceptor, you want to do the following:
public class MyInterceptor extends AbstractInterceptor {
/* interceptor intercept method */
public String intercept(ActionInvocation invocation) {
Long userId = getUserFromExternalSource();
injectUserIdIntoAction(invocation.getAction(), userId);
return invocation.invoke();
}
/* if the action implements IUserAware interface, inject the userid */
protected injectUserIdIntoAction(Object action, Long userId) {
if(action instanceof IUserAware) {
((IUserAware)action).setUserId(userId);
}
}
}
Then simply make sure your action implements the IUserAware interface.
This is how all the other Struts interceptor's work to set the request,
session, cookies, etc.
Chris
-----Original Message-----
From: Jyothrilinga Rao [mailto:[email protected]]
Sent: Thursday, August 18, 2011 1:44 PM
To: [email protected]
Subject: Set property for a action in interceptor
Hi,
I have a interceptor, which validates that the user is authenticated. It
does the authentication using a third party library and retrieves the
user id using the third party library.
I have a POJO action with propery userId. I want to set this property in
the interceptor, is this possible.
I have tried the following, but none of them see to work.
ActionContext ac = actionInvocation.getInvocationContext();
ac.put("userId", userId );
ac.getValueStack().setValue("userId", userId );
ac.getValueStack().push(userId);
Thanks,
Joe.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]