精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2005-04-06
针对“每次执行一个action都会导致重新创建interceptor对象”作了下改进
/* * 创建日期 2005-3-31 * */ package com.bupticet.strutsinterceptor; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.springframework.web.struts.DelegatingActionProxy; /** * <p>Title: InterceptorActionProxy</p> * * <p>Description: 拦截器代理Action类</p> * * <p>Copyright: Copyright (c);北京邮电大学网络教育技术研究所[www.buticet.com] 2005</p> * * <p>Company: 北京邮电大学网络教育技术研究所[www.buticet.com] </p> * * @author LJ-silver E-mail:LJ-silver@163.com * @version 1.0 */ public class InterceptorActionProxy extends DelegatingActionProxy { static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(InterceptorActionProxy.class);; /** * Pass the execute call on to the Spring-managed delegate Action. * @see #getDelegateAction */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response); throws Exception { Action delegateAction = getDelegateAction(mapping);; ActionForward forward = null; List interceptorList = InterceptorConfig.getInterceptorList(request);; int size = interceptorList.size();; if(size > 0);{ ActionInterceptor[] interceptorArray = InterceptorConfig.getInterceptor(interceptorList);; try{ for(int i = 0;i<size;i++);{ forward = interceptorArray[i].beforeAction(mapping, form, request, response);; if(forward!=null);{ return forward; } } forward = delegateAction.execute(mapping, form, request, response);; for(int i = 0;i<size;i++);{ interceptorArray[i].afterAction(mapping, form, request, response);; } }catch(Exception e);{ e.printStackTrace();; logger.error("In execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response);, Exception Occured ! Info :" + e.getLocalizedMessage(););; for(int i = 0;i<size;i++);{ forward = interceptorArray[i].throwsAction(mapping, form, request, response,e);; if(forward!=null);{ return forward; } } } }else{ forward = delegateAction.execute(mapping, form, request, response);; } return forward; } } |
|
返回顶楼 | |
发表时间:2005-04-06
/* * 创建日期 2005-3-31 * */ package com.bupticet.strutsinterceptor; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.struts.config.ActionConfig; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * <p>Title: InterceptorConfig</p> * * <p>Description: 配置文件读取类</p> * * <p>Copyright: Copyright (c);北京邮电大学网络教育技术研究所[www.buticet.com] 2005</p> * * <p>Company: 北京邮电大学网络教育技术研究所[www.buticet.com] </p> * * @author LJ-silver E-mail:LJ-silver@163.com * @version 1.0 */ public class InterceptorConfig { private static HashMap config; private static HashMap interceptor; private final static String filePathName = "/struts-interceptor.xml"; static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(InterceptorConfig.class);; public static void init();{ if(config==null);{ initResource();; } } private static void initResource();{ Document document = null; try{ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();; DocumentBuilder db = dbf.newDocumentBuilder();; InputStream is = InterceptorConfig.class.getResourceAsStream(filePathName);; document = db.parse(is);; Element root = document.getDocumentElement();; NodeList interceptors = root.getElementsByTagName("interceptor-definition");; Map map = new HashMap();; for(int i = 0;i<interceptors.getLength();;i++);{ Element interceptor = (Element); interceptors.item(i);; if(map.containsKey(interceptor.getAttribute("name");););{ logger.error("In initResource();, Exception Occured !Info :不能重复配置interceptor-definition元素:"+interceptor.getAttribute("name"););; }else{ map.put(interceptor.getAttribute("name");,interceptor.getAttribute("type"););; } } NodeList actions = document.getElementsByTagName("action");; config = new HashMap();; for(int i = 0;i<actions.getLength();;i++);{ Element action = (Element); actions.item(i);; String path = action.getAttribute("path");; NodeList methods = action.getElementsByTagName("method");; Map temp = new HashMap();; for(int j = 0;j<methods.getLength();;j++);{ Element method = (Element); methods.item(j);; String name = method.getAttribute("name");; List actionInterceptorList = new ArrayList();; NodeList actionInterceptors = method.getElementsByTagName("interceptor");; for(int k = 0;k<actionInterceptors.getLength();;k++);{ Element actionInterceptor = (Element); actionInterceptors.item(k);; String interceptorName = actionInterceptor.getAttribute("name");; if(map.containsKey(interceptorName););{ actionInterceptorList.add(map.get(interceptorName););; }else{ logger.error("In initResource();, Exception Occured !Info :没有"+interceptorName+"对应的interceptor-definition元素!" );; } } String[] allMethods = name.split(",");; for(int m = 0;m<allMethods.length;m++);{ if(temp.containsKey(allMethods[m]););{ logger.error("In initResource();, Exception Occured !Info :不能重复配置method元素:"+allMethods[m] );; }else{ temp.put(allMethods[m],actionInterceptorList);; } } } if(config.containsKey(path););{ logger.error("In initResource();, Exception Occured !Info :不能重复配置path元素:"+path );; }else{ config.put(path,temp);; } } }catch(IllegalArgumentException e);{ logger.error("In initResource();, Exception Occured ! 找不到文件/WEB-INF/classes/struts-interceptor.xml!,Info :" + e.getLocalizedMessage(););; e.printStackTrace();; }catch(SAXException e);{ logger.error("In initResource();, Exception Occured ! XML文件/WEB-INF/classes/struts-interceptor.xml语法错误!,Info :" + e.getLocalizedMessage(););; e.printStackTrace();; }catch(Exception e);{ logger.error("In initResource();, Exception Occured !Info :" + e.getLocalizedMessage(););; e.printStackTrace();; } } public static synchronized List getInterceptorList(String path,String method);{ init();; List list = new ArrayList();; if(config.containsKey(path););{ HashMap map = (HashMap);config.get(path);; if(map.containsKey(method););{ list = (ArrayList);map.get(method);; } } return list; } public static synchronized List getInterceptorList(HttpServletRequest request);{ ActionConfig actionConfig = (ActionConfig);request.getAttribute("org.apache.struts.action.mapping.instance");; String path = actionConfig.getPath();; String method = null; String parameter = actionConfig.getParameter();; if(parameter!=null&&!"".equals(actionConfig.getParameter();););{ method = request.getParameter(parameter);; } if(method==null||"".equals(method););{ method = "execute"; } return getInterceptorList(path,method);; } public static synchronized ActionInterceptor getInterceptor(String name); throws InstantiationException, IllegalAccessException, ClassNotFoundException{ if(interceptor == null);{ interceptor = new HashMap();; } if(interceptor.containsKey(name););{ return (ActionInterceptor);interceptor.get(name);; }else{ ActionInterceptor actionInterceptor = (ActionInterceptor);Class.forName(name);.newInstance();; interceptor.put(name,actionInterceptor);; return actionInterceptor; } } public static synchronized ActionInterceptor[] getInterceptor(List name); throws InstantiationException, IllegalAccessException, ClassNotFoundException{ ActionInterceptor[] actionInterceptor = new ActionInterceptor[name.size();]; for(int i = 0;i<name.size();;i++);{ actionInterceptor[i] = getInterceptor((String);name.get(i););; } return actionInterceptor; } public static void test();{ init();; for(Iterator it = config.keySet();.iterator();;it.hasNext();;);{ String key = (String);it.next();; System.out.println(key);; Map map = (HashMap);config.get(key);; for(Iterator ite = map.keySet();.iterator();;ite.hasNext();;);{ String key2 = (String);ite.next();; System.out.println("------------------------"+key2);; List list = (ArrayList);map.get(key2);; for(Iterator iter = list.iterator();;iter.hasNext();;);{ System.out.println("------------------------"+" "+"------------------------"+(String);iter.next(););; } } } } } |
|
返回顶楼 | |
发表时间:2005-04-06
/* * 创建日期 2005-4-6 * */ package com.bupticet.strutsinterceptor; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.config.ActionConfig; /** * <p>Title:PrintInfoInterceptor </p> * * <p>Description: 执行Action的前后以及异常抛出时均打印提示信息,用于调试</p> * * <p>Copyright: Copyright (c);北京邮电大学网络教育技术研究所[www.buticet.com] 2005</p> * * <p>Company: 北京邮电大学网络教育技术研究所[www.buticet.com] </p> * * @author LJ-silver E-mail:LJ-silver@163.com * @version 1.0 */ public class PrintInfoInterceptor implements ActionInterceptor{ static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(PrintInfoInterceptor.class);; /* * @see com.bupticet.strutsinterceptor.ActionInterceptor#beforeAction(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse); */ public ActionForward beforeAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response); throws IOException, ServletException { logger.info("====================before run======================");; logger.info("=====================================================================");; for(Enumeration e = request.getAttributeNames();;e.hasMoreElements();;);{ Object o = e.nextElement();; logger.info((String);o+":"+request.getAttribute((String);o););; } logger.info("=====================================================================");; ActionConfig actionConfig = (ActionConfig);request.getAttribute("org.apache.struts.action.mapping.instance");; String parameter = actionConfig.getParameter();; if(null!=request.getParameter(parameter););{ logger.info((String);request.getParameter(parameter););; } logger.info("=====================================================================");; return null; } /* * @see com.bupticet.strutsinterceptor.ActionInterceptor#afterAction(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse); */ public void afterAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response); throws IOException, ServletException { logger.info("====================after run======================");; } /* * @see com.bupticet.strutsinterceptor.ActionInterceptor#throwsAction(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Exception); */ public ActionForward throwsAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, Exception e); throws Exception { logger.info("====================throw run======================");; return null; } } |
|
返回顶楼 | |