Search code examples
javaportletjsf-1.2jsr286

How to create a portlet URL in JSF-Portlet


The goal is to create a URL to the Portlet with this code:

ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
RenderResponse response = (RenderResponse)ctx.getResponse();
PortletURL portletUrl = response.createRenderURL();
String url = portletUrl .toString();

But if I call this in a backing bean's JSF-actionListener method, I get a ClassCastException because ctx.getResponse() gives me an javax.portlet.ActionResponse instead.

I know that a RenderResponse is accessible from the doView method in the Portlet class. But how can I access it in my backing bean?


Solution

  • I use the following approach now, which causes some (very little) workload overhead, but works well:

    • Grab the RenderResponse in the doView method
    • Use PortletURL portletUrl = response.createRenderURL(); and store this object in the portlet session (with every request, I know).
    • In your action listener method, retrieve the PortletURL object, append neccessary parameters and render the URL for whatever.