[url]http://my.oschina.net/u/2007041/blog/420956[/url]
在项目中记录操作日志,是一种很常见的需求。
有时我们在service或者dao层记录日志,需要同时保存访问ip、登录用户名等。如果从controller层把HttpServletRequest 对象传过去会显得很麻烦。HttpSession可以通过HttpServletRequest 间接获取。
在web.xml配置
在service或者dao中获取HttpServletRequest 的代码如下
为了方便,可以如上代码提取到一个工具方法中,避免重复。
在项目中记录操作日志,是一种很常见的需求。
有时我们在service或者dao层记录日志,需要同时保存访问ip、登录用户名等。如果从controller层把HttpServletRequest 对象传过去会显得很麻烦。HttpSession可以通过HttpServletRequest 间接获取。
在web.xml配置
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
在service或者dao中获取HttpServletRequest 的代码如下
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
为了方便,可以如上代码提取到一个工具方法中,避免重复。