背景
最近,给系统添加用户的操作日志,用到了切面,需要获取相关参数,对于大部分码农来说,
还是习惯了使用HttpServletRequest对象
获取方式
@AfterReturning("logPoinCut()")
public void saveSysLog(JoinPoint joinPoint){
//获取request对象
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//.....其他操作
}
碰到的问题
1 在测试过程中,发现报空指针异常, ServletRequestAttributes对象为null
解决办法
1 在工程的web.xml中配置如下监听器
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>