Liferay’s asset framework supports a rating system that lets your users rate content in plugins. You’ve probably seen this in many of Liferay’s built-in plugins. A great example is the Blogs portlet. On viewing a blogs entry, a row of stars appears, letting the user rate the entry on a scale from one to five stars. Ratings help users figure out what content is popular. The feature also fosters a sense of community and collaboration among content producers and consumers. Even better, once your plugin is asset enabled, implementing ratings is a snap.
This tutorial shows you how to add ratings to an asset enabled portlet. The tutorial uses code from a custom Insults portlet as an example. The Insults portlet seemed appropriate, since a truly distinguished writer of insults needs to know how good his or her insults really are.
Now go ahead and get started learning how to add ratings to your portlets!
If you’ve implemented asset rendering for your custom entity, you can display ratings in the full content view of your entity. You can, of course, also display this asset feature in any view JSP you create for viewing the entity.
As an example, the Insult portlet’s view JSP file
shows an insult entity and its ratings. This tutorial shows you how to associate
the ratings component with your custom entity.
In your view JSP you can use
ParamUtil to get the ID of the entity from the
render request. Then you can create an entity object using your
<% long insultId = ParamUtil.getLong(renderRequest, "insultId"); Insult ins = InsultLocalServiceUtil.getInsult(insultId); %>
After the code that displays your entity, you can add the ratings component
liferay-ui:ratings tag. Note that the object’s ID is used to tie the
ratings to the entity:
<liferay-ui:ratings className="<%=Insult.class.getName()%>" classPK="<%=ins.getInsultId()%>" type="stars" />
In the code above, the
type attribute is given the value
"stars" to use the
five star rating system. You can optionally replace the star ratings with a
thumbs-up or thumbs-down rating system by changing this value to
If you haven’t already connected your portlet’s view to the JSP for your entity, you can refer here to see how to connect your portlet’s main view JSP to your entity’s view JSP.
Great! Now you have the JSP that lets your users rate content.
Now redeploy your portlet and refresh the page so that the your plugin’s UI reloads. The ratings UI component now appears in your entity’s view.
Great! Now you know how to add ratings for content in your asset enabled portlets.
Another thing you might want to do is perform permissions checks to make sure
only the proper users can rate content. For example, the Add Insult and
Permissions buttons of the Insults portlet are wrapped in a permissions check in
For more information on this, see the learning path
Checking for Permissions in the UI.