Foros de discusión

How to generate a Portlet URL?

Fernando Cabrera, modificado hace 9 años.

How to generate a Portlet URL?

Junior Member Mensajes: 47 Fecha de incorporación: 1/08/14 Mensajes recientes
Hello, I already post a question about this in the Development section of the forum: How to generate the most consistent Portlet URL?

But because my portlet is a JSF portlet, maybe here I found the anwser. The question is:

I would like to generate an absolute URL associated to the page where my custom portlet is, but not related to the current session/request (p_auth parameter) because I need that the URL to be available in future occasions.

To give more context to the question. I'm developing a portlet that generates user notifications, so at the end, the link is going to be in the dockbar, that is the reason to be absolute and "inter-session"

Current approach is:

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
ServiceContext serviceContext = ServiceContextFactory.getInstance(portletRequest);
String portletId = serviceContext.getPortletId();
long plid = serviceContext.getPlid();
String lifecycle = PortletRequest.ACTION_PHASE;
LiferayPortletURL portletURL = PortletURLFactoryUtil.create(portletRequest, portletId, plid, lifecycle);


And that generate some URL like this:

http://localhost:8081/web/guest/inicio?p_auth=1TNzy9Cz&p_p_id=notificationtestportlet_WAR_notificationtestportlet&p_p_lifecycle=1


What is the best way to generate the URL?

Thanks!
thumbnail
Vernon Singleton, modificado hace 9 años.

RE: How to generate a Portlet URL?

Expert Mensajes: 315 Fecha de incorporación: 14/01/13 Mensajes recientes
Hi Fernando,

Fernando Cabrera:
... generate an absolute URL associated to the page where my custom portlet is ...

So far, so good. I think I understand.

Fernando Cabrera:
To give more context to the question. I'm developing a portlet that generates user notifications, so at the end, the link is going to be in the dockbar

Just curious, after you generate the URL, how are you planning to add this URL to the dockbar? The answer to this seems important in your case, and would drive which method below you might use to generate your URL.

Fernando Cabrera:
What is the best way to generate the URL?

It sounds like you want to be able to programatically generate either 1) a URL to your JSF portlet, or 2) a URL to the page containing your JSF portlet. Is that correct? I am not sure which of those two you want, but it sounds like either of those would work for your case.

There is a tag you could put in your jsf portlet called portlet:renderURL, that would give you such a URL.

For the first method of generating a URL to your JSF portlet, here are two pieces of code that generate that kind of URL:
RenderURLRenderer.java
PortletURLRenderer.java

For the second method of generating a URL to a page containing your JSF portlet, you can look at this section from the jsf2-ipc-events-customers-portlet demo here. That section of code shows how to generate a URL to a page using the PortalUtil class given the ThemeDisplay and the PortalPageId.

Hope that helps,
Vernon
Fernando Cabrera, modificado hace 9 años.

RE: How to generate a Portlet URL?

Junior Member Mensajes: 47 Fecha de incorporación: 1/08/14 Mensajes recientes
Thank you Vernon for reply

I didn't wanted to give so much information in order to make the question more concrete and generic for other people, but now I think it is important.

What I'm developing its User Notifications, it seems like the only information about this theme is this entry blog. Let's imagine an scenario: two portal users with different roles, a teacher and a student, The teacher is working on the "evaluation-portlet" that is included in the "Evaluation" page, and at the moment he assign a grade to a student exam, the portlet generates a notification for the corresponding student to let him know that now he can consult his grade, so maybe the student is in the "Home" page and the notification action should redirect to the Evaluation page and show an specific view of the evaluation portlet based on some request parameters.

Notifications are accesible by Liferay Notification Portlet included by default in the classic theme and available to install it from Liferay marketplace. It looks like this:



That is the reason to say that links are in the dockbar, like the "Confirm" and "Ignore" buttons on the image.

Because every button has a different action purpose, the URL should include some parameters that I'm reading in Manage Bean. The rest of the URL generation code that I omitted in the original question is:

LiferayPortletURL portletURL = PortletURLFactoryUtil.create(portletRequest, portletId, plid, lifecycle);
portletURL.setParameter("_facesViewIdRender", facesView);// facesView its String parameter method like "/views/accept.xhtml"
if (parameters != null) {
    for (Parameter param : parameters) {
        portletURL.setParameter(param.getKey(), param.getValue());
    }
}
portletURL.setWindowState(WindowState.NORMAL);
portletURL.setPortletMode(PortletMode.VIEW);


To be honest, I dont know if an URL to the page that contains my JSF portlet works for this scenario, could I still reading URL request parameters in manage bean? I know I should try before ask but right now I'm doing other stuff in my job.

Another important thing is that everything is made in Java classes so I think the tag is not a solution, am I right?
thumbnail
Vernon Singleton, modificado hace 9 años.

RE: How to generate a Portlet URL?

Expert Mensajes: 315 Fecha de incorporación: 14/01/13 Mensajes recientes
Hi Fernando,

Fernando Cabrera:
To be honest, I dont know if an URL to the page that contains my JSF portlet works for this scenario, could I still reading URL request parameters in manage bean?


If you do it this way, then yes, your managed bean should be able to get at the request paarmeter (foo):
MimeResponse mimeResponse = (MimeResponse) portletResponse;
PortletURL renderURL = mimeResponse.createRenderURL();
renderURL.setParameter("foo", "1234");

// http://portals.apache.org/pluto/portlet-2.0-apidocs/javax/portlet/MimeResponse.html#createRenderURL()

Note that this URL must be created from a MimeResponse, and so it can only be created during a render request or a resource request. An action request will not be sufficient for this.

Fernando Cabrera:
Another important thing is that everything is made in Java classes so I think the tag is not a solution, am I right?

I just showed you the tag so that you could easily find the code behind it.

Hope that helps,
Vernon