[jsp]代码库
package com.wang.aop;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.wang.model.User;
public class Log {
public void before(JoinPoint jp){
// 获取session中当前用户
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
System.out.println(user.getAccount());
System.out.println(user.getUsername());
String ip = request.getRemoteAddr(); // 获取IP地址
System.out.println(ip);
Object[] objs = jp.getArgs(); // 获取方法的输入参数
for(int i=0;i<objs.length;i++){
System.out.println("参数" + i + ":" + objs[i]);
}
String className = jp.getTarget().getClass().getSimpleName(); // 获取包含当前方法的对象的名字
String methodName = jp.getSignature().getName(); // 获取方法名
System.out.println(className + "." + methodName + "()");
}
}
获取request还要在web.xml下配置
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
监听
by: 发表于:2017-12-26 10:40:29 顶(0) | 踩(0) 回复
??
回复评论