struts2源码改造之debug

struts2提供了debug功能,可以通过struts/webconsole.html或static/webconsole.html进行OGNL表达式调试,而这个页面无论是否将struts.devMode配置项设为true都可以访问,总感觉有点不爽。

下面对这个功能进行改进,只有当struts.devMode配置项设为true时才允许访问webconsole.html,在org.apache.struts2.dispatcher.ng下的ExecuteOperations类进行调整。




public class ExecuteOperations {

    private Dispatcher dispatcher;
    private boolean devMode = false;//新增加一个变量,判断是否为开发模式,默认为false

    @Deprecated
    public ExecuteOperations(ServletContext servletContext, Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
       //------------------获取Struts2的常量配置项struts.devMode 的值-------------------------------
        devMode = Boolean.valueOf(dispatcher.getContainer().getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
    }

    public ExecuteOperations(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
       //------------------获取Struts2的常量配置项struts.devMode 的值-------------------------------
        devMode = Boolean.valueOf(dispatcher.getContainer().getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
    }

    /**
     * Tries to execute a request for a static resource
     * @return True if it was handled, false if the filter should fall through
     * @throws IOException
     * @throws ServletException
     */
    public boolean executeStaticResourceRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // there is no action in this request, should we look for a static resource?
        String resourcePath = RequestUtils.getServletPath(request);

        if ("".equals(resourcePath) && null != request.getPathInfo()) {
            resourcePath = request.getPathInfo();
        }
       //-------------------------------------------------------------------------------
        /**
         * 如果不为开发模式,直接返回false,这里返回false时,会继续向下执行其它过滤器,
         * 返回true不向下执行,它表示由本方法自己向页面输出内容,
         * 详细见org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类
         * 这里直接过滤所有的内置资源路径,在使用了dojo标签时,由于会用到内置的静态资源(如css、js、图片等)会存在加载问题
         * 如果只过滤debug资源,侧需要检查resourcePath里面的路径信息,根据resourcePath里面所包含的路径信息作处理
         */
        if(!devMode){
            return false;
        }
        /*
        //只过滤webconsole.html页面
        if(!devMode && null!=resourcePath && resourcePath.indexOf("webconsole.html")!=-1){
            return false;
        }
        */
        //-------------------------------------------------------------------------------
        StaticContentLoader staticResourceLoader = dispatcher.getContainer().getInstance(StaticContentLoader.class);
        if (staticResourceLoader.canHandle(resourcePath)) {
            staticResourceLoader.findStaticResource(resourcePath, request, response);
            // The framework did its job here
            return true;

        } else {
            // this is a normal request, let it pass through
            return false;
        }
    }

    /**
     * Executes an action
     * @throws ServletException
     */
    public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
        dispatcher.serviceAction(request, response, mapping);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值