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>
</script>
For Free IT Study Materials,IT Certification Materials,Interview Questions and Job references.
ReplyDeletesappractices@gmail.com
For All SAP Modules.
Testing Tools
Bigdata Technologies
Oracle Technologies
IBM Technologies
BI Tools
Thanks