Search code examples
jspliferayliferay-6

Call jsp page from Liferay Portal


Im a newbie in Liferay development and I have a pretty simple question.

I have an example of portlet that shows a text message and I need to display in the Liferay Portal. How can I do this ? the JSP code is below:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
<%@ page import="javax.portlet.PortletPreferences" %> 

<portlet:defineObjects /> 

<% 
     PortletPreferences prefs = renderRequest.getPreferences();
     String greeting = (String)prefs.getValue( "greeting", "Hello! Welcome to our portal."); 
%>

<p><%= greeting %></p>

Solution

  • EDIT

    How can I write the same portlet without java code in view.jsp file?

    You must override MVCPortlets doView method to initialize an attribute, let's consider the attribute greeting. For example:

    public class Greeting extends MVCPortlet {
    
        @Override
        public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
    
            renderRequest.setAttribute("greeting", "Hello! Welcome to our portal.");
    
            super.doView(renderRequest, renderResponse);
        }
    }
    

    And in your JSP file, retrieve the greeting attribute using EL ${greeting}.

    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> 
    
    <portlet:defineObjects /> 
    
    <p>${greeting}</p>
    

    Ouput:

    Hello! Welcome to our portal.