简体   繁体   English

struts2拦截器重定向两次以执行操作

[英]struts2 interceptor redirecting twice to action

i have a logininterceptor that checks session member object and if it is null redirect it to login page,else continue to action . 我有一个用于检查会话成员对象的logininterceptor,如果为null,则将其重定向到登录页面,否则继续操作。

Every action blocks has this interceptor. 每个动作块都具有此拦截器。 The problem is When you call action it hits the interceptor, if it is true continue to action method, then return "success" or "input" then again hits the interceptor to redirect that selected result name. 问题是,当您调用动作时,它会击中拦截器,如果确实如此,则继续执行动作方法,然后返回“成功”或“输入”,然后再次击中拦截器以重定向所选结果名称。

How can i prevent to call interceptor twice? 如何防止两次调用拦截器?

Interceptor code : 拦截器代码:

public String intercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request = (HttpServletRequest) actionInvocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
        HttpSession session = request.getSession();

    if (session.getAttribute("member")==null) {

        return Action.LOGIN;
    } else {
        return actionInvocation.invoke();
    }
}

struts.xml action view : struts.xml操作视图:

    <action name="actionName" class="actionClass" method="init">
        <interceptor-ref name="loginStack"/>
        <result name="input">show.jsp</result>
    </action>

If you redirect to a result that has the same interceptor in its refs of course it will be called twice. 如果您重定向到引用中具有相同拦截器的结果,那么它将被两次调用。

If you forward to a JSP as you show here, the interceptor will not be called again. 如果您按照此处显示的方式转发到JSP,则不会再次调用拦截器。


There's no need to declare this for every action--set the default interceptor stack. 无需为每个操作都声明它-设置默认的拦截器堆栈。 For actions that don't need this stack (like login) define an interceptor ref for that action that doesn't include this interceptor. 对于那些并不需要这个堆栈(如登录)定义拦截裁判对那个动作不包括这个拦截行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM