What is the difference between super.doView(...) and include(...) on doView override?

liferay, portlet

Solution

So you must be extending `MVCPortlet` class. Both the calls are used to include the JSP after the `doView` processing is completed. If you look at the source code of this class then you would understand what the flow is, below is my explanation:

super.doView(renderRequest, renderResponse);

This includes the default JSP i.e. `view.jsp`, that you might (or not) have configured in `portlet.xml` something like this:

<init-param>
    <name>view-template</name>
    <value>/html/view.jsp</value>
</init-param>

This super class method does nothing but calls the `include(viewJSP, renderRequest, renderResponse);` method at the end.

include(viewJSP, renderRequest, renderResponse);

This method includes whatever JSP path you have specified for the parameter `viewJSP`. So with this call you can specify including different JSP for different condition something like the following:

if (isThisTrue) {
    include("/html/myCustomPortlet/view.jsp", renderRequest, renderResponse);
} else if (isThisTrueThen) {
    include("/html/myCustomPortlet/first/another_view.jsp", renderRequest, renderResponse);
} else {
    super.doView(renderRequest, renderResponse);
}

So depending on your requirement you can use any to the two or the mix of the two as shown above. Hope this helps.

Problem

I am developing a custom portlet (EDIT:I'm extending MVCPortlet), and looking at several examples and tutorials, I find that when the doView(RenderRequest, RenderResponse) method is overridden, at the end of it there is always at least this line: ``` super.doView(renderRequest, renderResponse); ``` or this: ``` include(viewJSP, renderRequest, renderResponse); ``` If I don't put either of these my portlet doesn't render anything, but any of them does the trick. I would like to know which one I should be using, and why do I need to add them to get my portlet to work. Thanks!

Original source