How to render Web content inside portlet – Liferay 6.2

In most of the real time application development, we may want to display dynamic content inside our custom portlet. This can be done by embedding a web content inside our custom portlet, as web contents can be modified on the fly and does not require any code changes or deployment. In this post, we will learn a way to embed a web content into our custom portlet and display the content on front end.

Basic steps to embed a web content to custom portlet are:

Create a Web content

Create a web content from control panel. Login with administrator credentials, navigate to Web content portlet on control panel section and add a new web content. This is the content we are going to embed on our portlet.

Create a custom portlet and embed the web content into it

Create a liferay portlet, open the view.jsp and add the following code into it. Here, we are fetching the web content based on the url title.

<%@ 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() %>

Finally, we have our web content getting displayed on our custom portlet.

Embed webcontent into portlet

Also Read:

Note: Version used: Liferay 6.2 + Apache Tomcat 7.