在Liferay中,如果能用好PortletPreferences那么会非常强大:
简介:
PortletPreferences是和当前用户相关的一组信息的结合,我们可以让一个已登陆用户在一个Portlet的不同页面,不同阶段通过这个PortletPreferences来共享数据,而无需担心其他用户去窃取这些数据。利用这个共享数据的好处是它不需要特定的实体存储,比如数据库,文件。
PortletPreferences分为2种,一种是静态的,一种是动态的.
静态的PortletPreference就是写在portlet.xml文件中的,如下:
- <portlet-preferences>
- <preference>
- <name>department</name>
- <value>liferay</value>
- </preference>
- <preference>
- <name>company</name>
- <value>cignex</value>
- </preference>
- </portlet-preferences>
我们在页面或者java代码中,可以用如下的代码来获取:
- PortletPreferences preferences = renderRequest.getPreferences();
- String department = preferences.getValue("departmentt", "");
动态PortletPreferences就强大太多了,举一个简单的需求,比如我们要在Portlet的configure模式吧一些参数设置进去,比如REST请求url,还有一些头参数等,然后我们要在view模式吧这些信息读取出来。
那么,我们可以这样做:
首先,自定义一个Helper类,比如叫PortletHelper,它定义一个静态方法叫getPortletPreference():
- public static PortletPreferences getPortletPreferences(ActionRequest actionRequest) throws PortalException, SystemException{
- PortletPreferences preferences = actionRequest.getPreferences();
- String portletResource = ParamUtil.getString(actionRequest, "portletResource");
- if (Validator.isNotNull(portletResource)) {
- preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);
- }
- return preferences;
- }
然后我们在config模式中新建一个PortletPreferences对象,然后把必要的内容填充进去:
- public void processAction(PortletConfig portletConfig,
- ActionRequest actionRequest, ActionResponse actionResponse)
- throws Exception {
- if(LOGGER.isDebugEnabled()){
- LOGGER.debug("ConfigurationAction : processAction()");
- }
- PortletPreferences preferences = PortletHelper.getPortletPreferences(actionRequest);
- try{
- FileOperationHelper.uploadFileToDest(actionRequest);
- preferences.setValue(RS_LAUNCH_PORTLET_PREFERENCE_HTML_URL, ConfigurationActionHelper.getHtmlUrl(actionRequest));
- preferences.setValue(RS_LAUNCH_PORTLET_PREFERENCE_REST_WS_URL, ConfigurationActionHelper.getRestUrl(actionRequest));
- preferences.setValue(RS_LAUNCH_PORTLET_PREFERENCE_REST_WS_TYPE, ConfigurationActionHelper.getRestType(actionRequest));
- preferences.setValue(SERVICE_VERSION, ConfigurationActionHelper.getRestVersion(actionRequest));
- preferences.setValue(SERVICE_ENV, ConfigurationActionHelper.getRestEnv(actionRequest));
- preferences.setValue(CONSUMER_AUTH_TOKEN, ConfigurationActionHelper.getRestToken(actionRequest));
- preferences.setValue(CONSUMER_ID, ConfigurationActionHelper.getRestConId(actionRequest));
- preferences.store();
- ..
最后我们在view模式中使用存储在PortletPreferences中的内容:
- @RenderMode(name = RS_LAUNCH_PORTLET_RENDER_MODE_VIEW)
- public void view(RenderRequest renderRequest, RenderResponse renderResponse)
- throws PortletException, IOException, PortalException,
- SystemException {
- String jsp = null;
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("RSLaunchPortlet : view()");
- }
- PortletHelper.createActionUrls(renderRequest, renderResponse);
- String portletInstanceId = (String) renderRequest
- .getAttribute(RS_LAUNCH_PORTLET_RENDER_REQUEST_ATTRIBUTE_PORTLET_ID);
- PortletPreferences preferences = renderRequest.getPreferences();
- String html_url = preferences.getValue(
- RS_LAUNCH_PORTLET_PREFERENCE_HTML_URL, StringPool.DOUBLE_QUOTE);
- String rest_url = preferences.getValue(
- RS_LAUNCH_PORTLET_PREFERENCE_REST_WS_URL,
- StringPool.DOUBLE_QUOTE );
- ..
这就达到了在与某个用户指定的Portlet之内共享部分数据的功能,还是很简单的。
转载于:https://blog.51cto.com/supercharles888/989524