Spring AOP(一)获取操作参数request、session、application_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Spring AOP(一)获取操作参数request、session、application

Spring AOP(一)获取操作参数request、session、application

 2010/12/25 0:04:36  liweitsky  http://liweitsky.javaeye.com  我要评论(0)
  • 摘要:springAOP的用途极其广泛,但在struts2之前request等获取是个问题,这里简单介绍一下我的方法:struts1中两种方法:1.通过threadlocal先写java类packagecom.util;importjava.io.Serializable;importjavax.servlet.http.HttpServletRequest;publicclassThreadTestimplementsSerializable{privatestaticThreadLocal<
  • 标签:Spring 操作
springAOP的用途极其广泛,但在struts2之前request等获取是个问题,这里简单介绍一下我的方法:
struts1中两种方法:
1.通过threadlocal
先写java类
package com.util; 
import java.io.Serializable; 

import javax.servlet.http.HttpServletRequest; 


public class ThreadTest implements Serializable{ 
      private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>(); 

public HttpServletRequest getContext(){ 
      return (HttpServletRequest)threadLocal.get(); 
} 
public void setContext(HttpServletRequest request){ 
      threadLocal.set(request); 
} 

public void cleanContext(){ 
      threadLocal.set(null); 
} 
} 

配置为filter
<filter>
	<filter-name>test</filter-name>
	<filter-class>com.util.ThreadTest</filter-class>
</filter>
<filter-mapping>
	<filter-name>test</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

最后在aop方法中只要包含HttpServletRequest request, HttpServletResponse response参数就可以了

方法二:
public Object saveLog(ProceedingJoinPoint joinPoint) throws Throwable {
		Object retVal = null;
		try {
			if(logDao == null){
				String[] locations={"applicationContext-beans.xml","applicationContext-actions.xml", "applicationContext-common.xml"};
				ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
				logDao = (LogDao) ctx.getBean("logDao");
			}
			//logDetail(jointPoint,":startTime:");
			retVal = joinPoint.proceed();

			Object[] args = joinPoint.getArgs();
			HttpServletRequest request = null;
			ActionMapping mapping = null;
                        //通过分析aop监听参数分析出request等信息
			for (int i = 0; i < args.length; i++) {
				if (args[i] instanceof HttpServletRequest) {
					request = (HttpServletRequest) args[i];
				}if (args[i] instanceof ActionMapping) {
					mapping = (ActionMapping) args[i];
				}
			}
			Person person = new Person();
			if (request != null) {
				person = (Person) request.getSession().getAttribute("login");
			}
			String method = request.getParameter("method");
			System.out.println("------------------------------------"+SystemContext.getOffset());
			//登录日志
			if (method.equals("login")) {
			}
			//查看
			if (method.equals("showAll")) {
			}
			}

			//logDetail(joinPoint,":endTime:");
		} catch (Exception ee) {
			System.out.println("出错:" + ee.toString());
		}
		return retVal;
	}


在struts2中request等作为系统参数,已经可以直接调用了
发表评论
用户名: 匿名