2014-01-15 32 views
0

我想在config mod中訪問portlet的上下文(在我的接口ConfigurationAction接口中)。如何獲取config(Liferay)中的portlet上下文?

我嘗試,因爲時間得到同樣的背景下,我ConfigurationActionImpl.processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse),因爲我有我的doView(RenderRequest renderRequest, RenderResponse renderResponse),但沒有任何好的結果。

在我doView(),我可以使用getPortletContext()(同getPortletConfig().getPortletContext())和renderRequest.getPortletSession()訪問我的portlet上下文(這是相同的上下文實例),但我不知道我怎麼可以訪問這些對象的一個​​我processAction()

有人可以幫我嗎?

+0

A是一種解決方法,我使用了一個** static **類變量(HashMap )。它滿足我的需求,但我不確定這是最佳做法。 – pierreonthenet

+0

Portlet類中的任何成員(無論是否爲靜態)通常都是潛在問題的指示符。 (我認爲這個假設適用於你的情況)。如果以不要求「如何達到我想到的解決方案」的方式提問您的問題可能會更容易 - 而是要求「我想實現X - 我該如何做到最好」 –

+0

謝謝爲了您的評論。我嘗試了你談論的方式[這裏](http://stackoverflow.com/questions/21087613/how-to-share-data-between-all-users-of-a-portlet-instance),但沒有真正解決這個問題。 – pierreonthenet

回答

0

這是我最後使用方法:

PortletBag portletBag = PortletBagPool.get(portletId); 
DispatcherPortlet portlet = (DispatcherPortlet)portletBag.getPortletInstance(); 

PortletContext pCtx = portlet.getPortletContext(); 

如果你想計算portletId,你可以這樣做:

String portletResource = ParamUtil.getString(renderRequest, "portletResource"); 
String portletId; 
if (portletResource.contains("_INSTANCE")) { 
    portletId = portletResource.substring(0, portletResource.indexOf("_INSTANCE")); 
} else { 
    portletId = portletResource; 
} 

作爲一個方面說明,我想提一提我需要它以便能夠獲得Portlet的彈簧ApplicationContext,我這樣做:

PortletContext pCtx = portlet.getPortletContext(); 
ApplicationContext portletAppContext = (ApplicationContext)pCtx.getAttribute(FrameworkPortlet.PORTLET_CONTEXT_PREFIX + portlet.getPortletName());