Friday, January 11, 2008

How to access the Portlet Mode from a JSF Portlet

We at JBoss a division of Red Hat use Hudson as a Continuous Integration tool. So I thought why not write a portlet that integrates with Hudson. The portlet can be downloaded from Portlet Swap. I ended up using the RI JSF implementation and JSF-Portlet bridge. It took me a while to figure out how to access some portal/portlet related objects in managed bean so I thought I should share what I found out.

To get the portlet mode in JSF's managed bean, you need to do
Object reqObj = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if  (reqObj instanceof PortletRequest)
request = (PortletRequest)reqObj;
return request.getPortletMode();

You can get PortletPreferences similary by using request.getPreferences().

To get init parameters that you have specified in portlet.xml, you use
Map applicationMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
if (null != applicationMap)
PortletConfig config = (PortletConfig)applicationMap.get("javax.portlet.PortletConfig");
return config.getInitParameter(param);

1 comment:

angelo said...

is there another way for getting portlet init parameters?
My problem is: I have two jsf portlets in the same web application (two portlets defined in portlet.xml), if I invoke first the portlet 1 and subsequently the portlet 2, I retrieve the init-params related to the first portlet invoked.

Thank you in advance.