为了实现模块化开发,struts_config.xml想放在各个plugin代码之下,如何扩展ActionServlet来读入这些分散的配置文件?
4条回答 默认 最新
- wayne_ren 2012-06-26 09:26关注
如果看过struts的源码,了解struts的初期加载过程的话,就该知道在struts中可以覆写ActionServlet的initModuleConfig方法来实现自己的加载封装。
举例说一下:
假如你的模块都放在WEB-INF\plugin之下:
[list][*]WEB-INF\plugin\cms\
[*]WEB-INF\plugin\blog\
[*]WEB-INF\plugin\member\
[*]WEB-INF\plugin\space\
[*]...[/list]而struts_config.xml文件就在每个plugin的根目录下:
如:WEB-INF\plugin\cms\struts_config.xml那么就可以如下扩展ActionServlet来实现自定义加载:
[code="java"]
public class BaseServlet extends ActionServlet {protected ModuleConfig initModuleConfig(String prefix, String paths) throws ServletException { // Parse the configuration for this module ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); ModuleConfig config = factoryObject.createModuleConfig(prefix); // Configure the Digester instance we will use Digester digester = initConfigDigester(); String path = getServletContext().getRealPath("/"); String pluginPath = path + "WEB-INF" + File.separator + "plugin"; try { File[] files = new File(pluginPath).listFiles(); for (int intCnt = 0; intCnt < files.length; intCnt++) { if (files[intCnt].isDirectory()) { digester.push(config); String configPath = files[intCnt].getAbsolutePath() + File.separator + "struts_config.xml"; if(new File(configPath).exists()) { String pp = configPath.substring(getServletContext().getRealPath("/").length() - 1).replace('\\', '/');; System.out.println(pp); parseModuleConfigFile(digester, getServletContext().getResource(pp)); } } } } catch (MalformedURLException e) { throw new UnavailableException(e.getMessage()); } getServletContext().setAttribute( Globals.MODULE_KEY + config.getPrefix(), config); return config; }
}
[/code]本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记