在MainServlet中,初始化Portlets之后,就开始初始化布局模板了,对应代码是:if (_log.isDebugEnabled()) { _log.debug("Initialize layout templates"); } try { initLayoutTemplates(pluginPackage, portlets); } ..
它会调用initLayoutTemplates方法:
protected void initLayoutTemplates( PluginPackage pluginPackage, List<Portlet> portlets) throws Exception { ServletContext servletContext = getServletContext(); String[] xmls = new String[] { HttpUtil.URLtoString( servletContext.getResource( "/WEB-INF/liferay-layout-templates.xml")), HttpUtil.URLtoString( servletContext.getResource( "/WEB-INF/liferay-layout-templates-ext.xml")) }; LayoutTemplateLocalServiceUtil.init( servletContext, xmls, pluginPackage); }首先,第05行取得servlet上下文,然后第07-14行去ROOT下的/WEB-INF中去找2个XML配置文件,我看了下Liferay CE版本下的liferay-layout-template.xml,发现这里定义了2套模板,一套是<standard>,一套是<custom>,,每套下又细分为各种小模板,每种模板都用tpl文件来定义。
然后在第16行,调用LayoutTemplateLocalServiceUtil.init方法来读取这些xml配置文件,并且进行初始化这些模板:
public static java.util.List<com.liferay.portal.kernel.util.ObjectValuePair<java.lang.String, java.lang.Boolean>> init( java.lang.String servletContextName, javax.servlet.ServletContext servletContext, java.lang.String[] xmls, com.liferay.portal.kernel.plugin.PluginPackage pluginPackage) { return getService() .init(servletContextName, servletContext, xmls, pluginPackage); }它最终会调用LayoutTemplateLocalServiceImpl类的init()方法:
public List<ObjectValuePair<String, Boolean>> init( String servletContextName, ServletContext servletContext, String[] xmls, PluginPackage pluginPackage) { List<ObjectValuePair<String, Boolean>> layoutTemplateIds = new ArrayList<ObjectValuePair<String, Boolean>>(); try { for (int i = 0; i < xmls.length; i++) { Set<ObjectValuePair<String, Boolean>> curLayoutTemplateIds = _readLayoutTemplates( servletContextName, servletContext, xmls[i], pluginPackage); Iterator<ObjectValuePair<String, Boolean>> itr = curLayoutTemplateIds.iterator(); while (itr.hasNext()) { ObjectValuePair<String, Boolean> ovp = itr.next(); if (!layoutTemplateIds.contains(ovp)) { layoutTemplateIds.add(ovp); } } } } catch (Exception e) { _log.error(e, e); } return layoutTemplateIds; }