[英]Liferay theme and portlet communication
我在主題中嵌入了一個Portlet。 我發現允許主題從Portlet獲取參數值的唯一解決方案是使用中間數據庫。 我所做的是在portlet中創建了一個表,然后嘗試從主題訪問該表:
Portlet中的Java代碼:
ExpandoTable table=null;
try {
table = ExpandoTableLocalServiceUtil.addTable(CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId(), User.class.getName(), "ClientTab");
}
catch ( DuplicateTableNameException dtne) {
table=ExpandoTableLocalServiceUtil.getTable(CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId(), User.class.getName(), "ClientTab");
}
主題中的速度代碼:
#set ($accountsTableName = "ClientTab")
#set ($accountsTable = $expandoTableLocalService.getTable($accountsTableName, $accountsTableName))
#if (!$accountsTable)
<h2> The table ClientTab doesn't exist </h2>
#else
<h2> Well The table ClientTab exists </h2>
#end
但是我得到的結果是:
表ClientTab不存在
我使用這些引用來開發我的代碼:
http://myjavaexp.blogspot.com/2013/01/liferay-expando-services.html
http://www.liferay.com/fr/web/raymond.auge/blog/-/blogs/715049
如果只想將值從portlet傳遞到主題,則可以在render方法中添加新的Velocity變量,如下所示:
HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(request);
Map<String, Object> velocityVariables = (Map<String, Object>)httpRequest.getAttribute(WebKeys.VM_VARIABLES);
if(velocityVariables == null) {
velocityVariables = new HashMap<String, Object>();
}
velocityVariables.put("mySpecialKey", "mySpecialValue");
request.setAttribute(WebKeys.VM_VARIABLES, velocityVariables);
然后,您可以像這樣在主題中使用變量:
<h1>Here's the value added by my portlet --> $mySpecialKey</h1>
不要忘記變量前面的$
字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.