0

我是Java的初学者。我想验证我的会话,如果没有退出会话,没有人将能够登录。我正在通过拦截器执行此操作,并且我已经创建了拦截器类,其中如果会话对象为 null 并且如果会话对象不等于 null,那么它将调用该操作但actionInvocation.invoke()不工作,并且它给出空指针异常。

public class AuthenticationInterceptor implements Interceptor{
    private static final long serialVersionUID = 1L; 

    public AuthenticationInterceptor() {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        String vString=null;
      try{
            HttpServletRequest request = (HttpServletRequest) actionInvocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
             HttpSession session = request.getSession();
             System.out.println(session.getAttribute(Constant.CURRENT_USER));
             UserDTO userDTO=(UserDTO) session.getAttribute(Constant.CURRENT_USER);

        if(userDTO == null){
            System.out.println("userdto=null"+userDTO);
            vString = "login";
        }
        else if(!(userDTO).equals(null)){
                System.out.println("userdto!!!=null"+userDTO.getUserId());

                System.out.println("test...   "+actionInvocation.invoke());
                vString= actionInvocation.invoke();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }return vString;

        }

    private void addActionError(Object action, String message) {
        if (action instanceof ValidationAware) {
            ((ValidationAware) action).addActionError(message);
        }
    }

}

下面是xml文件..

<interceptors>
            <interceptor name="authenticate" class="wm.com.erp.utility.AuthenticationInterceptor"/>

<interceptor-stack name="authenticateStack">
                <interceptor-ref name="authenticate"/>
            </interceptor-stack>
        </interceptors>

        <global-results>
            <result name="login" type="redirect">/index.jsp</result>
        </global-results>
<action name="Assignment" class="wm.com.erp.action.AssignmentAction" method="AssignmentList">
            <interceptor-ref name="authenticateStack"/>

            <result name="success">/Assignment.jsp</result>
            <result name="input">/home.jsp</result>
        </action>

您的帮助将不胜感激。

4

1 回答 1

0
Map<String,Object> session = invocation.getInvocationContext().getSession();

if(session.isEmpty())
      return Action.ERROR; // session is empty/expired error must be redirect on

return invocation.invoke();

如果您有问题,请在此之后输入此代码,然后请发布您的异常的堆栈跟踪

于 2013-04-19T09:25:35.180 回答