创建和配置过滤器
1.第一步:过滤器是实现
??? package interceptor;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import utils.StringContonts;
import model.UserInfo;
public class AutoInterceptor extends MethodFilterInterceptor {
??? @Override
??? protected String doIntercept(ActionInvocation ai) throws Exception {
??? ??? // TODO Auto-generated method stub
??? ??? Map<String, Object> session = ai.getInvocationContext().getSession();
??? ??? //当前角色id
??? ??? UserInfo userInfo =(UserInfo) session.get(StringContonts.SESSION_USER);
??? ??? String path=ServletActionContext.getServletContext().getContextPath();
??? ??? String realPath=ServletActionContext.getRequest().getServletPath();
??? ??? //登录验证
??? ??? if(userInfo == null&&!realPath.contains("loginAdmin")) {
??? ??? ??? HttpServletResponse res=ServletActionContext.getResponse();
??? ??? ??? res.setCharacterEncoding("UTF-8");
??? ??? ??? if(realPath.contains("personcenter")){
??? ??? ??? ??? res.getWriter().write("<script>alert("你未登录或登录已超时,请重新登录!");self.parent.parent.location=""+path+"";</script>");
?????????????? // return "toPerson";
??? ??? ??? ??? return null;
??? ??? ??? }else if(realPath.contains("manage")){
??? ??? ??? ??? res.getWriter().write("<script>alert("你未登录或登录已超时,请重新登录!");self.parent.parent.location=""+path+"/administrator/login.jsp"</script>");
? // return "toAdmin";
??? ??? ??? ??? return null;
??? ??? ??? }else{
??? ??? ??? ??? return ai.invoke();
??? ??? ??? }
??? ??? ???
??? ??? }
??? ??? return ai.invoke();
??? }
}
2. 第二步 配置过滤器
<package name="sdzn" extends="struts-default">
??? <interceptors>
??? ??? ??? <!-- 权限拦截器 -->
??? ??? ?<interceptor name="lauthority" />
??? ??? ??? ??? ??? <interceptor-ref name="defaultStack" />
??? ??? ??? ??? </interceptor-stack>
??? ??? </interceptors>
??? ??? <default-interceptor-ref name="lcsfAuthStack" />
??? ??? <global-results>
??? ??? ??? <!-- 没通过登录验证 -->
??? ??? ??? <result name="toAdmin" type="redirect">/administrator/login.jsp</result>
??? ??? ??? <result name="toPerson" type="redirect">/forepage/counties/index.jsp</result>
??? ??? ??? <!-- 错误页面 -->
??? ??? ??? <result name="error">/administrator/error.jsp</result>
??? ??? </global-results>
??? </package>
?