Struts2拦截器的ActionInvocation对象的getAction方法无法获取到Acti

2020-10-10 15:40发布

如以下代码:

public class MyIntercepter extends AbstractInterceptor {

  public String intercept(ActionInvocation ai) throws Exception {
    User user = (User) ActionContext.getContext().getSession().get("user");
    if (user!=null) {
      return ai.invoke();
    }else{
      LoginAction loginAction = (LoginAction) ai.getAction();
      loginAction.addActionError("你没登录,请先登录");
    return Action.INPUT;
  }
}

}

报错:java.lang.ClassCastException: com.opensymphony.xwork2.ActionSupport cannot be cast to com.ibm.action.LoginAction

按理说这样写没错啊 - -,ActionSupport类的实例竟然不能赋给 继承ActionSupport的类实例吗

 

struts.xml配置:

<action name="*">
  <result>/WEB-INF/content/{1}.jsp</result>
  <result name="input">/WEB-INF/content/login.jsp</result>

  <interceptor-ref name="defaultStack"></interceptor-ref>
  <interceptor-ref name="myIntercepter"></interceptor-ref>
</action>

标签:
1条回答
forever°为你锁心
2楼-- · 2020-10-10 16:16

<action name="*"><!--原因出现在这里,没有配置对应的class-->
  <result>/WEB-INF/content/{1}.jsp</result>
  <result name="input">/WEB-INF/content/login.jsp</result>

  <interceptor-ref name="defaultStack"></interceptor-ref>
  <interceptor-ref name="myIntercepter"></interceptor-ref>
</action>

查看更多
登录 后发表回答