如何将数据从Interceptor传递到URL和JSP?

发布于 2024-09-19 03:25:49 字数 934 浏览 3 评论 0原文

我有一个身份验证拦截器来检查用户是否登录。 如果没有,它将重定向到登录页面,并使用查询字符串参数“url”指示引用网址。我尝试使用“actionInvocal.getInspirationContext().getParameters()”将值传递到重定向 URL,但没有运气。

谁能建议我做错了什么?多谢。

拦截器代码:

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部分

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

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 URL, 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放手` 2024-09-26 03:25:49

检查 struts.xml 文件中的拦截器顺序; params 拦截器应该放在登录拦截器之前:

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

这是一个 示例

获取查询字符串的另一种方法是从属性javax.servlet.forward.query_string

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文