通过login.action访问,并输入了正确的用户名和密码也被拦截了
struts中只有名为athu的action引用了拦截器,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 声明包 -->
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="athu" class="com.zzy.interceptor.AthuInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="athu"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="athu">
<result name="success">/WEB-INF/pages/manager.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
<action name="login" class="com.zzy.action.UserAction" method="login">
<result name="success">/WEB-INF/pages/manager.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
public String login(){
if("admin".equals(username)&&"admin".equals(password)){
session.put("username",username);
return SUCCESS;
}else{
session.put("login_error", "用户名或密码不正确");
return "login";
}
}
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext actionContext=ActionContext.getContext();
Map<String, Object> session=actionContext.getSession();
if(session.get("username")!=null){
String result=invocation.invoke();
return result;
}else{
return "login";
}
}