Liferay provides taglibs that enable comments on your app’s content. Here, you’ll learn how to use these taglibs, using a sample Guestbook app as an example.
Follow these steps to enable commenting on your app’s content:
-
Make sure your entity is asset enabled.
-
Choose a read-only view of the entity you want to enable comments on. You can display the comments component in your app’s view, or if you’ve implemented asset rendering you can display it in the full content view in the Asset Publisher app.
-
Include the
liferay-ui
,liferay-comment
, andportlet
taglib declarations in your JSP:<%@ taglib prefix="liferay-ui" uri="http://liferay.com/tld/ui" %> <%@ taglib prefix="liferay-comment" uri="http://liferay.com/tld/comment" %> <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>
-
Use
ParamUtil
to get the entity’s ID from the render request. Then create an entity object using the-LocalServiceUtil
class. Here’s an example that does this for a guestbook entry in the example Guestbook app:<% long entryId = ParamUtil.getLong(renderRequest, "entryId"); entry = EntryLocalServiceUtil.getEntry(entryId); %>
-
Create a collapsible panel for the comments using the
liferay-ui:panel-container
andliferay-ui:panel
tags. This lets users hide the discussion area:<liferay-ui:panel-container extended="<%=false%>" id="guestbookCollaborationPanelContainer" persistState="<%=true%>"> <liferay-ui:panel collapsible="<%=true%>" extended="<%=true%>" id="guestbookCollaborationPanel" persistState="<%=true%>" title="Collaboration">
-
Create a URL for the discussion using the
portlet:actionURL
tag:<portlet:actionURL name="invokeTaglibDiscussion" var="discussionURL" />
-
Use the
liferay-comment:discussion
tag to add the discussion. To let the user return to the JSP after making a comment, set the tag’sredirect
attribute to the current URL. You can usePortalUtil.getCurrentURL((renderRequest))
to get the current URL from therequest
object. In this example, the current URL was earlier set to thecurrentURL
variable:<liferay-comment:discussion className="<%=Entry.class.getName()%>" classPK="<%=entry.getEntryId()%>" formAction="<%=discussionURL%>" formName="fm2" ratingsEnabled="<%=true%>" redirect="<%=currentURL%>" userId="<%=entry.getUserId()%>" /> </liferay-ui:panel> </liferay-ui:panel-container>
If you haven’t already connected your portlet’s view to the JSP for your entity, see Configuring JSP Templates for an Asset Renderer.