zoukankan      html  css  js  c++  java
  • 关于拦截器实现日志存储到db的代码调试

    问题是,原来系统有日志操作的代码,但日志最终没有存到数据库。

    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();

    在网上查找资料后觉得这样写有问题。

    http://zhidao.baidu.com/link?url=eeQHgVchoECim2s5SZPKE-qasm0ylP5O4NOD7GUnLRZg9KP1aew1LkMX6dN7BxR0aFXxJYiaMQZqmmXqCuziu_

    如上文所述,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

  • 相关阅读:
    实例教程五:采用SharedPreferences保存用户偏好设置参数
    实例教程四:采用Pull解析器解析和生成XML内容
    实例教程六:创建数据库与完成数据添删改查第一种写法
    实例教程二:短信发送器
    实例教程九:采用ContentProvider对外共享数据
    带手势滑动的日历Demo
    实例教程三:文件的保存与读取
    短信快速回复(源码)
    实例教程八:采用ListView实现数据列表显示
    javascript中的变量申明
  • 原文地址:https://www.cnblogs.com/rixiang/p/4931220.html
Copyright © 2011-2022 走看看