标签:
问题是,原来系统有日志操作的代码,但日志最终没有存到数据库。
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 + "\n"); } } } } } Log log = new Log(); log.setOperation(operation); log.setOperator(operator); log.setContent(content); log.setParameter(parameter.toString()); log.setIp(ip); logService.save(log); break; } } } }
调试代码时发现:path的值一直为“”。因为为“”,所以进入不了if (antPathMatcher.match(logConfig.getUrlPattern(), path))的判断语句中,自然执行不了里面logService.save(log)的语句。
AntPathMatcher是spring的工具类,具体可百度。
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(); String url = request.getRequestURL().toString(); String path = url.substring(16); 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 + "\n"); } } } } } Log log = new Log(); log.setOperation(operation); log.setOperator(operator); log.setContent(content); log.setParameter(parameter.toString()); log.setIp(ip); logService.save(log); break; } } } }
解决了。
标签:
原文地址:http://www.cnblogs.com/rixiang/p/4931220.html