开发者

How to pass data from Interceptor to URL and JSP?

开发者 https://www.devze.com 2023-01-15 08:34 出处:网络
I have an authen interceptor that check if user is logged in. If not than it will redirect to the login page, with a query string param \"url\" indicating the referrer URL. I tried using \"actionInvoc

I have an authen interceptor that check if user is logged in. If not than it will redirect to the login page, with a query string param "url" indicating the referrer URL. I tried using "actionInvocation.getInvocationContext().getParameters()" for passing values to the redirect UR开发者_Go百科L, but have no luck.

Can anyone suggest what I done wrong? Thanks a lot.

Interceptor code:

public String intercept(ActionInvocation actionInvocation) throws Exception {
    Map session = actionInvocation.getInvocationContext().getSession();
    Map params = actionInvocation.getInvocationContext().getParameters();

    String user = (String) session.get(Constants.KEY_USER);

    boolean isAuthenticated = (null!=user);

    if (!isAuthenticated) {
        params.put("backUrl", "http://www.some_url.com/");
        return Action.LOGIN;            
    }
    else {
        return actionInvocation.invoke();
    }
}

struts.xml parts

<global-results>
   <result name="login" type="redirect">/login?url=${backUrl}</result>


Check the order of the interceptors in your struts.xml file; params interceptor should be placed before login interceptor:

 <interceptor-stack name="defaultLoginStack">
      ...
      <interceptor-ref name="params" />
      <interceptor-ref name="login" />
      ...
 </interceptor-stack>

Here's an example

Another way to get the query string, is from the attribute javax.servlet.forward.query_string:

 String queryString = (String) actionInvocation.getRequest().
      getAttribute("javax.servlet.forward.query_string");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号