Liferay DXP’s asset framework supports a system for rating content in apps. This feature appears in many core apps, such as the Blogs portlet. Ratings give your users a voice, and you can benefit from their feedback. Thanks to Liferay DXP’s taglibs, you can enable ratings for your app in only a few lines of code.
Follow these steps:
-
Make sure your entity is asset enabled.
-
Choose a read-only view of the entity for the ratings. You can display them in one of your portlet’s views, or if you’ve implemented asset rendering you can display them in the full content view in the Asset Publisher portlet.
-
In the JSP, include the
liferay-ui
taglib declaration:<%@ taglib prefix="liferay-ui" uri="http://liferay.com/tld/ui" %>
-
Use
ParamUtil
to get the entity’s ID from the render request. Then use the-LocalServiceUtil
class to create an entity object:<% long entryId = ParamUtil.getLong(renderRequest, "entryId"); entry = EntryLocalServiceUtil.getEntry(entryId); %>
-
Use the
liferay-ui:ratings
tag to add the ratings component:<liferay-ui:ratings className="<%=Entry.class.getName()%>" classPK="<%=entry.getEntryId()%>" type="stars" />
The
type
attribute specifies the type of rating system to display:- like: a likes rating system
- stars: a five star rating system
- thumbs: a thumbs-up or thumbs-down rating system, as shown in the figure above
Although the ratings type is specified here, you can make the type configurable for administrators by Implementing Ratings Type Selection and Value Type Transformation in your app.
Great! Now you know how to let users rate content in your asset-enabled portlets.