Setting page headers from JSR 286 portlet

News Site news RSS

Latest articles New articles RSS

Latest news from Twitter Follow Secretkeeper4 and www.svesoftware.com on Twitter

Setting page headers from JSR 286 portlet

The portlets are isolated islands within a portal page. Important issue as result of this isolation is how to set page title dynamically from the portlet, or how to set cookies from the portlet.

This is possible using JSR286 portlet specification.

Before we continue with the example, we have to enable RENDER_HEADERS lifecycle with following container runtime option: javax.portlet.renderHeaders. This option has to be set in portlet.xml file for each portlet in which we want to use this lifecycle.
The sample environment is WebSphere Portal 7. 

 

<portlet>...
<container-runtime-option>
        <name>javax.portlet.renderHeaders</name>
        <value>true</value>
    </container-runtime-option>
</portlet>

The method that we have to override is:

 

protected void doHeaders(RenderRequest request, RenderResponse response)
{
   Element title = response.createElement("title");
   title.setTextContent("Title of the page");
   response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, title); 

}

 
and the page title is set from the GenericPortlet class. This is very simple.
 
However, if you are using Spring 3.0 or greater, doHeaders cannot be easily overriden.
Instead, DispatcherPortlet has to be extended like this:
 
public class ExtendedDispatcherPortlet extends DispatcherPortlet {
   protected void doHeaders(RenderRequest request, RenderResponse response) {
      super.doHeaders(request, response);
      Element title = response.createElement("title");
      title.setTextContent("Title");
      response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, title);
   }
}

The new dispatcher has to be included as portlet class instead standard dispatcher portlet.