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等作为系统参数,已经可以直接调用了