struts源码学习5

来源:互联网 发布:ubuntu修改登录界面 编辑:程序博客网 时间:2024/06/05 08:05

ActionServlet.init()

1、initModuleForwards

配置全局forwards

    protected void initModuleForwards(ModuleConfig config)        throws ServletException {        if (log.isDebugEnabled()) {            log.debug("Initializing module path '" + config.getPrefix()                + "' forwards");        }        // Process forwards extensions.        //获取到ForwardConfigs        ForwardConfig[] forwards = config.findForwardConfigs();        for (int i = 0; i < forwards.length; i++) {            ForwardConfig forward = forwards[i];            //处理继承,实际上需要配置extends才有效,但是已被dtd限制,和前面的formbean一样            processForwardExtension(forward, config, null);        }        for (int i = 0; i < forwards.length; i++) {            ForwardConfig forward = forwards[i];            //必须配置path,否则抛异常            // Verify that required fields are all present for the forward            if (forward.getPath() == null) {                handleValueRequiredException("path", forward.getName(),                    "global forward");            }        }    }
2、initModuleExceptionConfigs

配置全局异常处理

 protected void initModuleExceptionConfigs(ModuleConfig config)        throws ServletException {        if (log.isDebugEnabled()) {            log.debug("Initializing module path '" + config.getPrefix()                + "' forwards");        }        //异常处理配置        // Process exception config extensions.        ExceptionConfig[] exceptions = config.findExceptionConfigs();        for (int i = 0; i < exceptions.length; i++) {            ExceptionConfig exception = exceptions[i];            processExceptionExtension(exception, config, null);        }        for (int i = 0; i < exceptions.length; i++) {            ExceptionConfig exception = exceptions[i];<span style="white-space:pre"></span>//必须配置key            // Verify that required fields are all present for the config            if (exception.getKey() == null) {                handleValueRequiredException("key", exception.getType(),                    "global exception config");            }        }    }

3、initModuleActions

    protected void initModuleActions(ModuleConfig config)        throws ServletException {        if (log.isDebugEnabled()) {            log.debug("Initializing module path '" + config.getPrefix()                + "' action configs");        }        // Process ActionConfig extensions.        ActionConfig[] actionConfigs = config.findActionConfigs();                for (int i = 0; i < actionConfigs.length; i++) {            ActionConfig actionConfig = actionConfigs[i];            processActionConfigExtension(actionConfig, config);        }        for (int i = 0; i < actionConfigs.length; i++) {            ActionConfig actionConfig = actionConfigs[i];            // Verify that required fields are all present for the forward            // configs            //forwards处理            ForwardConfig[] forwards = actionConfig.findForwardConfigs();            for (int j = 0; j < forwards.length; j++) {                ForwardConfig forward = forwards[j];                if (forward.getPath() == null) {                    handleValueRequiredException("path", forward.getName(),                        "action forward");                }            }            //异常处理            // ... and the exception configs            ExceptionConfig[] exceptions = actionConfig.findExceptionConfigs();            for (int j = 0; j < exceptions.length; j++) {                ExceptionConfig exception = exceptions[j];                if (exception.getKey() == null) {                    handleValueRequiredException("key", exception.getType(),                        "action exception config");                }            }        }    }
4、moduleConfig.freeze

该方法调用moduleConfig中所有config的freeze方法(比如actionConfig,controllerConfig等等)

调用该方法之后,所有的Config均不能调用setXX方法,一旦调用,将抛出异常

5、init其余代码

在拿到其它ServletConfig参数后,遍历

 Enumeration names = getServletConfig().getInitParameterNames();            while (names.hasMoreElements()) {                String name = (String) names.nextElement();<span style="white-space:pre"></span>//如果name已config开头,则认为是一个新的ActionServlet配置,这个ActionServlet配置是带prefix的。之前的配置中是没有prefix的                if (!name.startsWith(configPrefix)) {                    continue;                }                String prefix = name.substring(configPrefixLength);                moduleConfig =                    initModuleConfig(prefix,                        getServletConfig().getInitParameter(name));                initModuleMessageResources(moduleConfig);                initModulePlugIns(moduleConfig);                initModuleFormBeans(moduleConfig);                initModuleForwards(moduleConfig);                initModuleExceptionConfigs(moduleConfig);                initModuleActions(moduleConfig);                moduleConfig.freeze();            }            this.initModulePrefixes(this.getServletContext());            this.destroyConfigDigester();



0 0
原创粉丝点击