Portlet名字文本:
对于有紫色正方形标示的Portlet名字文本,比如我们这里的(ClusterNodeInfoPortlet),对照页面:
因为没有 portletItemId属性,所以我们断定走的是以下代码:
<div class="lfr-portlet-item <c:if test="<%= portletLocked %>">lfr-portlet-used</c:if> <c:if test="<%= portletInstanceable %>">lfr-instanceable</c:if>" id="<portlet:namespace />portletItem<%= portlet.getPortletId() %>" instanceable="<%= portletInstanceable %>" plid="<%= plid %>" portletId="<%= portlet.getPortletId() %>" title="<%= PortalUtil.getPortletTitle(portlet, application, locale) %>" > <p><%= PortalUtil.getPortletTitle(portlet, application, locale) %> <a href="javascript:;"><liferay-ui:message key="add" /></a></p> </div>所以,显示文本就在 PortalUtil.getPortletTitle方法中,而它最终会调用PortalImpl的getPortletTitle方法中:
public String getPortletTitle( Portlet portlet, ServletContext servletContext, Locale locale) { PortletConfig portletConfig = PortletConfigFactoryUtil.create( portlet, servletContext); ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale); return resourceBundle.getString(JavaConstants.JAVAX_PORTLET_TITLE); }所以,文本实际在09行的getString方法中获取的。我们跟进到ResourceBundle的getString(key)方法,它会调用ResourceBundle的getObject(key)方法:
public final Object getObject(String key) { Object obj = handleGetObject(key); if (obj == null) { if (parent != null) { obj = parent.getObject(key); } ... }进而会调用PortletResourceBundle的handleGetObject(key)方法:
protected Object handleGetObject(String key) { .. if ((value == null) || (value == ResourceBundleUtil.NULL_VALUE)) { value = _getJavaxPortletString(key); } .. return value; }从这里看出因为满足03行的条件,所以会调用PortletResourceBundle的_getJavaxPortletString(key)方法:
private String _getJavaxPortletString(String key) { if (key.equals(JavaConstants.JAVAX_PORTLET_TITLE)) { return _portletInfo.getTitle(); } else if (key.equals(JavaConstants.JAVAX_PORTLET_SHORT_TITLE)) { return _portletInfo.getShortTitle(); } else if (key.equals(JavaConstants.JAVAX_PORTLET_KEYWORDS)) { return _portletInfo.getKeywords(); } else if (key.equals(JavaConstants.JAVAX_PORTLET_DESCRIPTION)) { return _portletInfo.getDescription(); } return null; }因为我们传入的参数为 JavaConstants.JAVAX_PORTLET_TITLE,所以返回的结果是_portletInfo.getTitle()的字符串值。这个值是在PortletInfo构造函数���被赋值的:
public PortletInfo( String title, String shortTitle, String keywords, String description) { _title = title; _shortTitle = shortTitle; _keywords = keywords; _description = description; }而这个构造函数的最终也是在PortletLocalServiceImpl的_readPortletXML方法中被调用的:
private void _readPortletXML( String servletContextName, Map<String, Portlet> portletsPool, PluginPackage pluginPackage, PortletApp portletApp, Set<String> portletIds, long timestamp, Element portletElement) { Element portletInfoElement = portletElement.element("portlet-info");..if (portletInfoElement != null) { portletInfoTitle = portletInfoElement.elementText("title"); portletInfoShortTitle = portletInfoElement.elementText( "short-title"); portletInfoKeyWords = portletInfoElement.elementText("keywords"); } PortletInfo portletInfo = new PortletInfo( portletInfoTitle, portletInfoShortTitle, portletInfoKeyWords, portletInfoDescription); ..
从这里可以看出,这个title是在08行从中获取<title>元素,而这个title元素是从portletElement中获取<portlet-info>元素得来的。