In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to communicate between liferay and portlet, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Passing values through URL
This is a solution I found out when I encountered this problem * times in the project. The principle is that An adds the parameters needed by B to the self-generated URL, and then view the entire page once, and B does the corresponding processing according to the obtained parameters. Take the URL parameter code as follows:
Java code
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);} else return currentURL.substring (paramIndex + param.length () + 1, afaterParamSperatorIndex) } catch (RuntimeException e) {/ / TODO Auto-generated catch block return ";}} public static String getString (String currentURL, String param, String defaultStr) {String value = getString (currentURL, param); if (Validator.isNull (value)) return defaultStr Else return 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); else return 0;}
This method has many defects, such as the parameter can only be string, if it is map,list, big data is not feasible, there is another problem, security is not high, you can not predict how many portlet on this page, how many operations each portlet will have, and each operation will generate a url, will there be the same key? The probability of making mistakes is high.
Second, pass the value through session
The principle of this method is the same as that of the other methods, but it is safer. A triggers an action, and after the work to be dealt with is done in action, B puts what B needs in session, and B gets these things in render, and then completes his own work, or just finishes the work in JSP. But in this way, there is a problem. You can't know whether A will finish it first or B will finish it first. There are two ways to solve this problem. One is to let B wait for a period of time between processing. At this time, A must finish the work. The other is to solve it in the way of lock. Build a static map,A to open the lock, B to lock it, and make no mistake when the lock is empty or false. B just go and wait until the lock is opened. But be sure to destroy the lock after processing B is complete. I have only used this method once in a project.
Third, by simulating the PortletURL of B
The latter two methods are also what I have only learned in the past six months, and they should be safe. The previous code is the key to simulating the PortletURL of B
Java code
Private static long _ getPlidFromPortletId (HttpServletRequest request, long groupId, boolean isPrivate, String portletId, Entry... Entry) {long plid = 0; if (entry = = null) {plid = PortalUtil.getPlidFromPortletId (groupId, isPrivate, portletId); if (Validator.isNull (plid)) plid = Long.valueOf (0) } else {List 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 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; iTunes +) {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 block e.printStackTrace () }} return plid;} public static PortletURL getPortletURL (long groupId, String portletId, HttpServletRequest request, Entry... 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...) Entry) {return new PortletURLImpl (request, portletId, plid, PortletRequest.RENDER_PHASE);}
As can be seen from the above code, the most important thing is to obtain plid. If you have two portlet that need to communicate on the same page, it will save a big deal, otherwise you will have to call _ getPlidFromPortletId. * A parameter is used to match the configartion information of this portlet, because a website has more than a dozen pages, and three of them may have this portlet. Increasing this parameter can improve accuracy, but affect performance. PortletURL got, then the latter thing is easy to do, if you are A just to send the message itself without any processing, put the parameters into PortletURL, and then use it, if A wants to finish his work and then pass it, then finish his work in Action, and then sendRedirect to PortletURL.toString () on it. This approach should be relatively safe and reliable, and I have implemented it in several projects. Note: this scheme can only support portlet of B attribute false.
IV. Asynchronous communication through jQuery.ajax
This is a solution of * *. I have used this scheme for the ongoing project. The basic idea is: a background informs B to work, and then A foreground asks B to display the work result. This scheme is cool. The specific implementation is as follows:
1. A request B's jsonAction via jQuery.ajax (refer to the previous Liferay async submission article)
two。 Then according to the returned result, let B display the processing result (refer to the second type in the previous Liferay asynchronous refresh article)
Note: if you An also want to deal with the work, let A deal with it first and then call B's jsonAction,*** according to the result so that An and B both display their respective work results. Of course, you can also put all the work An and B do in a jsonAction.
Thank you for reading this article carefully. I hope the article "how to communicate between liferay and portlet" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.