Display Controller data in view in liferay

controller, jsp, liferay, view

Solution

There are a couple of options to achive this depending on the framework you use. (Liferay MVC Portlet , spring portlets , JSF)

For this answer I assume you use the MVC Portlet:

Lets go with everyones favorite example hello world:

public class HelloWorldPortlet extends MVCPortlet{

    @Override
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {

        renderRequest.setAttribute("helloMessage", "Hello World");
        super.doView(renderRequest, renderResponse);
    }


}

Now this won't work out of the box!

In your portlet.xml change:

<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

To:

<portlet-class>**Your Package structure**.HelloWorldPortlet</portlet-class>

The view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

<portlet:defineObjects />

<%-- Option A --%>
<h1>
    <c:out value="${helloMessage}" />
</h1>

<%-- Option B --%>
<% 
        String message = (String)request.getAttribute("helloMessage");
%>
<h1>
    <%= message %>
</h1>

Custom action:

View.jsp

<portlet:actionURL name="worldHello" var="worldpageURL" />

<aui:a href="${worldpageURL}">World hello</aui:a>

<h2><c:out value="${worldHello}"/></h2>

HelloWorldPortlet

public class HelloWorldPortlet extends MVCPortlet{

    @Override
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {

        renderRequest.setAttribute("helloMessage", "Hello World");
        super.doView(renderRequest, renderResponse);
    }



    public void worldHello(ActionRequest request, ActionResponse renderResponse){
        request.setAttribute("worldHello", "World Hello");
    }
}

Based on your comments it would be best if you look into :

http://www.liferay.com/community/liferay-projects/liferay-faces/documentation Since JSF based portlet could be there main focus in the future

http://www.liferay.com/community/blogs?p_p_id=115&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_115_struts_action=%2Fblogs_aggregator%2Fview_entry&_115_entryId=13443886

Problem

I looked at in everywhere and couldn't find it, or my search was not good enough. Anyways, here goes my question. How do I display and/or transfer data from a controller to the view jsp file in liferay MVC? i.e. if a variable "var" contains the value "This is a variable" in the controller java class, how do I display and/or access it in the jsp file? or to make it simple... How can we pass a value from controller to the jsp view in liferay MVC? Thank for any suggestions

Original source