因为我们以前项目中经常遇到配置了Portlet但是在对话框中找不到的情况,所以我们就来细致分析这里显示的文本是从哪里得来的。
对于整个dialog,它的代码是/html/portlet/layout_configuration/view_category.jsp.我们只想关注2个,一个是分类从哪里来,一个是Portlet 从哪里来:
分类文本:
对于黑色字的分类(比如我们这里的WalmartPlatformPortalDemo),其对应的代码在view_category.jsp中如下:
<h2> <span><%= Validator.isNotNull(externalPortletCategory) ? externalPortletCategory : LanguageUtil.get(pageContext, portletCategory.getName()) %></span> </h2>所以它最后会去调用LanguageUtil的get方法,而它最终内容会根据你的locale去读取resource bundle资源包,而传入的portletCategory.getName()则是当资源包中找不到匹配项时候的defaultName,因为大多数名字(我们为分类起的名字)都是项目特有的,不太可能和资源包中的key重名,所以大多数情况下最终显示的内容就是portletCategory.getName()返回的字符串值。
所以我们现在焦点在于portletCategory.getName()值是如何取来的。
在view_category.jsp的上方我们找到了:
<%@ include file="/html/portlet/layout_configuration/init.jsp" %> <% PortletCategory portletCategory = (PortletCategory)request.getAttribute(WebKeys.PORTLET_CATEGORY); ..所以,我们一定想到,这个request域上的属性值设置肯定是在包含这个jsp的文件中给出的。因为view_category.jsp中是被循环引入到layout_configuration/view.jsp的,果然在view.jsp中我们找到了对应的代码:
PortletCategory portletCategory = (PortletCategory)WebAppPool.get(company.getCompanyId(), WebKeys.PORTLET_CATEGORY); portletCategory = _getRelevantPortletCategory(permissionChecker, portletCategory, panelSelectedPortlets, layoutTypePortlet, layout, user); List categories = ListUtil.fromCollection(portletCategory.getCategories()); categories = ListUtil.sort(categories, new PortletCategoryComparator(locale)); int portletCategoryIndex = 0; Iterator itr = categories.iterator(); while (itr.hasNext()) { PortletCategory curPortletCategory = (PortletCategory)itr.next(); if (curPortletCategory.isHidden()) { continue; } request.setAttribute(WebKeys.PORTLET_CATEGORY, curPortletCategory); ...所以解开我们的钥匙就在WebAppPool的get方法,它的值肯定是设置进去的(否则怎么取呢),所以我们找到WebAppPool的put方法:
public static void put(Long webAppId, String key, Object obj) { _instance._put(webAppId, key, obj); }而这个方法是被PortalInstances的_initCompany方法所调用的:
private long _initCompany(ServletContext servletContext, String webId) { ... try { String xml = HttpUtil.URLtoString(servletContext.getResource( "/WEB-INF/liferay-display.xml")); PortletCategory portletCategory = (PortletCategory)WebAppPool.get( companyId, WebKeys.PORTLET_CATEGORY); if (portletCategory == null) { portletCategory = new PortletCategory(); } PortletCategory newPortletCategory = PortletLocalServiceUtil.getEARDisplay(xml); portletCategory.merge(newPortletCategory); for (int i = 0; i < _companyIds.length; i++) { long currentCompanyId = _companyIds[i]; PortletCategory currentPortletCategory = (PortletCategory)WebAppPool.get( currentCompanyId, WebKeys.PORTLET_CATEGORY); if (currentPortletCategory != null) { portletCategory.merge(currentPortletCategory); } } WebAppPool.put( companyId, WebKeys.PORTLET_CATEGORY, portletCategory); } ..