首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

创造和配置过滤器

2012-08-31 
创建和配置过滤器1.第一步:过滤器是实现??? package interceptorimport java.util.Mapimport javax.serv

创建和配置过滤器

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>

?

热点排行