liferay portlet之间的通信
从我做Liferay到现在,这个问题一直都存在着,直到最近我才算是找到了一个可靠的方案,能够满足各种需求,从我最开始做到现在一共用到了四种方案,从低到高,我们一个一个的讲。
一、通过URL传值
这个是我在项目中第一次遇到这样的问题,找出的一个方案,原理就是A把B需要的参数加在自已生成的URL后面,然后再把整个页面view一次,B根据取得的参数再做相应的处理,取URL参数代码如下:
public class CurrentURLUtil {public static Log log = LogFactory.getLog(CurrentURLUtil.class);public static int contain(String currentURL, String param) {return currentURL.indexOf(param);}public static String getString(String currentURL, String param) {try {int paramIndex = contain(currentURL, param);if (paramIndex == -1) {// log.warn("CurrentURL don't contain the parameter that name// is:"+param+",and method will return a blank");return "";} else {int afaterParamSperatorIndex = currentURL.indexOf("&", paramIndex+1);if (afaterParamSperatorIndex == -1) {return currentURL.substring(paramIndex + param.length() + 1);} elsereturn currentURL.substring(paramIndex + param.length() + 1,afaterParamSperatorIndex);}} catch (RuntimeException e) {// TODO Auto-generated catch blockreturn "";}}public static String getString(String currentURL, String param, String defaultStr) {String value = getString(currentURL, param);if(Validator.isNull(value))return defaultStr;elsereturn value;}public static long getLong(String currentURL, String param) {String value = getString(currentURL, param);if (null == value || value.trim().equals("")) {return 0;} else if(Validator.isNumber(value))return Long.parseLong(value);elsereturn 0;}}private static long _getPlidFromPortletId(HttpServletRequest request, long groupId, boolean isPrivate, String portletId, Entry<String, String>... entry) {long plid = 0;if (entry == null) {plid = PortalUtil.getPlidFromPortletId(groupId, isPrivate, portletId);if(Validator.isNull(plid))plid = Long.valueOf(0);} else {try {List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(groupId, isPrivate, LayoutConstants.TYPE_PORTLET);for (Layout layout : layouts) {LayoutTypePortlet layoutTypePortlet =(LayoutTypePortlet)layout.getLayoutType();if (layoutTypePortlet.hasPortletId(portletId)) {if (PortalUtil.getScopeGroupId(layout, portletId) == groupId) {plid = layout.getPlid();List<PortletPreferences> list = PortletPreferencesLocalServiceUtil.getPortletPreferences(plid, portletId);if (Validator.isNotNull(list)) {for (PortletPreferences pre : list) {int i = 0;if (entry.length < 1){plid = pre.getPlid();//_plidCache.put(key, plid);return plid;}javax.portlet.PortletPreferences jpre = PortletPreferencesSerializer.fromXML(PortalUtil.getCompanyId(request), pre.getOwnerId(), pre.getOwnerType(), plid,portletId, pre.getPreferences());for (; i < entry.length; i++) {Entry en = entry[i];if (!jpre.getValue(en.getKey().toString(),StringPool.BLANK).equals(en.getValue().toString()))break;}if (i == entry.length){plid = pre.getPlid();//_plidCache.put(key, plid);return plid;}}}}}}} catch (SystemException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return plid;}public static PortletURL getPortletURL(long groupId, String portletId,HttpServletRequest request, Entry<String, String>... entry) {long plid = _getPlidFromPortletId(request, groupId, false, portletId, entry);return new PortletURLImpl(request, portletId, plid,PortletRequest.RENDER_PHASE);}public static PortletURL getPortletURL(long groupId, long plid, String portletId,HttpServletRequest request, Entry<String, String>... entry) {return new PortletURLImpl(request, portletId, plid,PortletRequest.RENDER_PHASE);}