How to create a portlet URL in JSF-Portlet
Question
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 2
I use the following approach now, which causes some (very little) workload overhead, but works well:
- Grab the
RenderResponse
in thedoView
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.
OTHER TIPS
I'd like to put link into an email that leads the user to the portlet. I'm using WebSphere Portal 6.1.
The render URL is not normally available at that point in the portlet lifecycle.
- Consider using the URL mapping features
- Consider using a servlet with the URL engine service to perform a redirect
In both these cases, you don't need the huge encoded URL usually generated by Portal; you use something like http://host/foo/bar
as your entry point.
I've used the second approach in production. Unique names are added to the target pages and portlet instances for easy lookup. These are added to the page configuration via XMLAccess scripts - they are not available via the admin user interface in version 6.1.