我是Java的初学者。我想验证我的会话,没有exixting会话,没有人能够登录。我通过拦截器执行此操作,并且我已经创建了拦截器类,如果会话对象为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>
非常感谢你的帮助。
答案 0 :(得分: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();
如果您遇到问题,请在此之后输入此代码,然后请发布您的异常的堆栈跟踪