问题是,原来系统有日志操作的代码,但日志最终没有存到数据库。
xml中拦截器配置:
<mvc:interceptor> <mvc:mapping path="/admin/**" /> <bean id="logInterceptor" class="com.store.interceptor.LogInterceptor" /> </mvc:interceptor>
LogInterceptor:
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { List<LogConfig> logConfigs = logConfigService.getAll(); if (logConfigs != null) { String path = request.getServletPath(); for (LogConfig logConfig : logConfigs) { if (antPathMatcher.match(logConfig.getUrlPattern(), path)) { String username = adminService.getCurrentUsername(); String operation = logConfig.getOperation(); String operator = username; String content = (String) request .getAttribute(Log.LOG_CONTENT_ATTRIBUTE_NAME); String ip = request.getRemoteAddr(); request.removeAttribute(Log.LOG_CONTENT_ATTRIBUTE_NAME); StringBuffer parameter = new StringBuffer(); Map<String, String[]> parameterMap = request .getParameterMap(); if (parameterMap != null) { for (Entry<String, String[]> entry : parameterMap .entrySet()) { String parameterName = entry.getKey(); if (!ArrayUtils.contains(ignoreParameters, parameterName)) { String[] parameterValues = entry.getValue(); if (parameterValues != null) { for (String parameterValue : parameterValues) { parameter.append(parameterName + " = " + parameterValue + " "); } } } } } Log log = new Log(); log.setOperation(operation); log.setOperator(operator); log.setContent(content); log.setParameter(parameter.toString()); log.setIp(ip); logService.save(log); break; } } } }
在LogInterceptor中的postHandle方法中,日志存储的代码是写在if(antPathMatcher.match(logConfig.getUrlPattern(), path))中。但logConfig.getUrlPattern()的路径无法和得到的path路径匹配。调试后发现,path始终为“”。
所以重点看path。path的定义为String path = request.getServletPath();
在网上查找资料后觉得这样写有问题。
如上文所述,request.getServletPath()得到的是web.xml里面写的servlet路径。如果web.xml里面写的是/*这样的通配符的话,就显示“”了吧。
不管怎样将String path = request.getServletPath()改为
String path =request.getRequestURI(); 运行正常。
The referenced article:
http://www.aiuxian.com/article/p-252688.html