2015-01-08 110 views
0

我有一個portlet嵌入到主題中。我發現允許主題從portlet獲取參數值的唯一解決方案是使用中間數據庫。 我所做的是我在portlet中創建一個表,然後我試圖從主題訪問該表:Liferay主題和portlet通信

的Java代碼在portlet:

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.programcreek.com/java-api-examples/index.php?api=com.liferay.portlet.expando.DuplicateColumnNameException

http://www.liferay.com/fr/web/raymond.auge/blog/-/blogs/715049

+0

有沒有錯誤?你確定你不缺少參數:#set($ accountsTable = $ expandoTableLocalService.getTable($ accountsTableName,$ accountsTableName))'。另外爲什麼不顯示'$ accountsTable'來檢查。 –

回答

0

如果你只是想從您的portlet傳遞值到您可以在渲染方法中添加新速度變量的主題,如下所示:

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> 

不要忘記在變量前面的$字符。

+0

我發現我的問題。但是謝謝你的回答很好,我會在將來使用它。 –

+0

@KALLELOmar如果這不是你的問題的答案,那麼你可以自己寫答案,並提出這個答案,而不是將其標記爲答案。 –