Guys !!! Checkout my new application. Just upload your error/log file and trace all the exceptions along with detailed output in various formats (like xml, text, json).

Want To Search Something Else? Just Google It !

Sunday, October 2, 2011

Opening Portlet with Maximized Window in Render Mode

There is no provision in portlet API for opening a portlet with maximized window in render mode (doView() method). When a portlet gets loaded into browser for the first time, it always opens up in normal window.

I happened to come across a scenario where I had to open-up portlet in a maximized window which I achieved in following way:

javax.portlet.PortletURL object allows to set Window State. We can utilize this facility in following way to open a portlet with maximized window in render (doView() method) mode.

1. In doView method, Generate a render URL from renderResponse.
2. Add all the request parameters from renderRequest to these new URL.
3. Set window state to Maximized on this new URL.
4. Set this new URL value as renderRequest attribute and pass the control to view.jsp (your jsp file which generates output in render mode).
5. In view.jsp, retreive the URL we created in step-1 and redirect the page to this new URL using javascript by setting location.href value.

That's it. Next time, when the portlet will be rendered, it will open up in Maximized mode.

Following is the piece of code for both doView() method of Portlet class and view.jsp.

In Portlet Class,

public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException
    {
      
        if(renderRequest.getWindowState() != WindowState.MAXIMIZED)
        {
            //Create a render URL
            PortletURL url = renderResponse.createRenderURL();
          
            //Pass all the parameters from existing request to this new URL
            Enumeration params = renderRequest.getParameterNames();
          
            while(params.hasMoreElements())
            {
                String pname = params.nextElement().toString();
                url.setParameter(pname, renderRequest.getParameter(pname));
            }
          
            //Set window state to Maximized on newly created URL
            url.setWindowState(WindowState.MAXIMIZED);
          
            //Set this URL as attribute in renderRequerst
            renderRequest.setAttribute("maximizedUrl", url.toString());
          
          
            renderRequest.setAttribute("isMaximized", "false");
          
        }
        include(viewJSP, renderRequest, renderResponse);
    }
   

protected void include(String path, RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException
{
            PortletRequestDispatcher portletRequestDispatcher =
                                               getPortletContext().getRequestDispatcher(path);
            if (portletRequestDispatcher == null)
            {
                Logger.error(path + " is not a valid include");
            }
            else
            {
                portletRequestDispatcher.include(renderRequest, renderResponse);
            }
}


In view.jsp,

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

<jsp:useBean class="java.lang.String" id="maximizedUrl"
    scope="request" />
<jsp:useBean class="java.lang.String" id="isMaximized"
    scope="request" />

<portlet:defineObjects />


<script type="text/javascript">
    var maximizedFlag = '<%=isMaximized%>';
   
    //Redirect to New Render URL only if current window is not in maximized state
    if(maximizedFlag=='false')
    {
        location.href='<%=maximizedUrl%>';  
    }
</script>