Foros de discusión

What is the correct way to extend a portlet jspf using a hook?

Sharmini Sarvananda, modificado hace 9 años.

What is the correct way to extend a portlet jspf using a hook?

New Member Mensajes: 6 Fecha de incorporación: 14/09/10 Mensajes recientes
I would like to hide the Announcements portlet when there is no content to display (similar to how the Alerts portlet already behaves out of the box). I am able to do so by writing a hook that overwrites /html/portlet/announcements/view_entries.jspf. However I do not want to have to overwrite the whole jspf, I would prefer to extend the jspf instead. I followed the Liferay documentation here on how to extend a jsp. However, if I use the include as directed in the instructions,
<liferay-util:buffer var="html">
    <liferay-util:include page="/html/portlet/announcements/view_entries.portal.jspf" />
</liferay-util:buffer>

what happens is that the html is just printed out within the confines of the portlet on the page and all I see is a bunch of markup in the portlet. I find that if I change the include to view_entries.portal.JSP, the html is not spat out on the screen and everything looks to be working correctly, but the changes I want to add in never get added in. I am trying to do some string manipulation using a regular expression to insert some code into the html variable (see my code below). The html variable is always empty when the hook executes. Is this the correct way to extend a jspf? What am I doing wrong?

&lt;%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %&gt;

&lt;%@ page import="com.liferay.portal.kernel.util.StringUtil,java.util.regex.Matcher,java.util.regex.Pattern" %&gt;

<liferay-util:buffer var="html">
    <liferay-util:include page="/html/portlet/announcements/view_entries.portal.jsp" />
</liferay-util:buffer>

&lt;%
System.out.println("HTML IS: "+html);
String myCode = 
	     "renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);" 
	+ "}"
	+ "if (((hiddenMessagesCount == 0) &amp;&amp; (visibleMessagesCount == 0)) &amp;&amp; portletName.equals(PortletKeys.ANNOUNCEMENTS) &amp;&amp; !AnnouncementsEntryPermission.contains(permissionChecker, layout, PortletKeys.ANNOUNCEMENTS, ActionKeys.ADD_ENTRY)) {"
	+ 	 "renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, Boolean.TRUE);";
html = html.replaceFirst("^renderRequest\\.setAttribute\\(WebKeys\\.PORTLET_CONFIGURATOR_VISIBILITY.*;$", myCode);

%&gt;

&lt;%= html %&gt;
thumbnail
Olaf Kock, modificado hace 9 años.

RE: What is the correct way to extend a portlet jspf using a hook?

Liferay Legend Mensajes: 6396 Fecha de incorporación: 23/09/08 Mensajes recientes
You cannot change the code a JSP executes through string replacements, only the resulting HTML code (as your variable is correctly named)

Further, there's no "correct" way to extend JSPs (or jspf) in hooks - only more or less well maintainable strategies. If you want to do your replacement, you can't match for the actual java code that's in the JSP, you'll have to go for the pure HTML code. Please understand that JSPs are compiled into servlets, then into bytecode and executed. (check this in tomcat's work directory - quite eye-opening, though ugly machine-generated code).

Using liferay-util:buffer provides easy maintenance - e.g. if Liferay changes its implementation, you'll also get the changes. However, naturally your own string operation might be harder to see as it depends on the text that you're matching - which can be not-so-obvious.

Does this help?
Sharmini Sarvananda, modificado hace 9 años.

RE: What is the correct way to extend a portlet jspf using a hook?

New Member Mensajes: 6 Fecha de incorporación: 14/09/10 Mensajes recientes
Thank you Olaf! I have successfully written a different hook to extend a different jspf that does exactly the same thing as this one, and it works very well, the Java code does in fact get added in and is executed. I am confused as to why one should work and the other one does not.. Hence my question about if there is a standard way that can always be counted on to work when extending a jspf.
thumbnail
Olaf Kock, modificado hace 9 años.

RE: What is the correct way to extend a portlet jspf using a hook?

Liferay Legend Mensajes: 6396 Fecha de incorporación: 23/09/08 Mensajes recientes
I don't know if I fully answered your question about the "standard way" - it fully depends on the amount of changes that you have: If you want to add a little bit of text, <liferay-util:buffer> is a good way to do this. If you want to change the appearance more thoroughly, copying the original JSP and then change it can be beneficial. Or, you could completely re-implement the fuctionality and change the view.

As of the first two options, you'll have to decide if you prefer making sense of your string replacement (potential RegEx) once Liferay's JSP is updated or if you rather do a three way merge. It depends on the complexity of the string operation IMHO
Sharmini Sarvananda, modificado hace 9 años.

RE: What is the correct way to extend a portlet jspf using a hook?

New Member Mensajes: 6 Fecha de incorporación: 14/09/10 Mensajes recientes
Thank you Olaf. I finally figured out what I was doing wrong. I was trying to extend a JSPF and thought that I could use the same string manipulation strategy as for a JSP. However, this is not possible as Liferay is doing a dynamic include of the JSPF. I will stick to the overwrite strategy for modifying JSPF files. Thank you very much for your help, it is much appreciated!