How To Render Web Content Inside Portlet – Liferay 6.2

In most real-time applications, we may want to render some dynamic content inside our custom Liferay portlet. We can embed web content inside our custom portlet, as web contents can be modified on the fly and do not require any code changes or deployment.

In this post, we will learn how to embed Liferay web content into our custom portlet and display the web content on the portlet page.

Version details: Liferay version 6.2

Create and render web content in liferay portlet

Create a new Liferay web content from the control panel. Log in with the administrator credentials, navigate to the Web content portlet on the control panel section, and add new Liferay web content.

After that, We are going to embed the newly created Liferay web content on our Liferay portlet.

Please Create a new Liferay portlet, open the view.jsp page of the created portlet project, and add the below code into it.

<%@ taglib uri="" prefix="portlet" %>
<%@ taglib uri="" prefix="portlet" %>

<%@page import="com.liferay.portlet.journalcontent.util.JournalContentUtil"%>
<%@page import="com.liferay.portlet.journal.model.JournalArticleDisplay"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@page import="com.liferay.portal.kernel.util.WebKeys" %>
<%@page import="com.liferay.portlet.journal.model.JournalArticle" %>
<%@page import="com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil" %>

<portlet:defineObjects />

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); 
JournalArticle article =  JournalArticleLocalServiceUtil.getLatestArticleByUrlTitle(themeDisplay.getScopeGroupId(), "my-first-web-content", 0); 
JournalArticleDisplay articleDisplay = JournalContentUtil.getDisplay(article.getGroupId(), article.getArticleId(), null , themeDisplay.getLanguageId(), themeDisplay); 
String content = articleDisplay.getContent();

This is the <b>EmbedWcPortlet</b> portlet.<br/><br/>
<h4> Here is our Web content:</h4> 
<%=content.toString() %>

We are retrieving the Liferay web content using the JournalArticleLocalServiceUtil class.

We are sending the URL title of the Liferay web content and the group id of the theme as parameters.

Also, We are using the JournalArticleDisplay returned by the JournalContentUtil class to get the displayable web content.

Finally, we are printing the content string by calling the getContent() method of the JournalArticleDisplay instance.

Testing the output

Run the application and add the portlet to the Liferay page.

The below image shows our custom portlet that displays the embedded web content.

embed web content inside portlet


In this article, we learned how to create the Liferay web content and then add it inside a Liferay portlet.