[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-user
Subject:    RE: Set property for a action in interceptor
From:       "CRANFORD, CHRIS" <Chris.Cranford () setech ! com>
Date:       2011-08-18 20:34:22
Message-ID: 415DBA0D5EE0C24D9197CC6063E7502968CE40 () shqsr15a ! setechusa ! com
[Download RAW message or body]

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:jyothril@gmail.com] 
Sent: Thursday, August 18, 2011 1:44 PM
To: user@struts.apache.org
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic