Vista combinada Visión Plana Vista de árbol
Discusiones [ Anterior | Siguiente ]
toggle
Enzo Ferrari
How to get PortletPreferences from servlet
26 de diciembre de 2012 7:29
Respuesta

Enzo Ferrari

Ranking: New Member

Mensajes: 19

Fecha de incorporación: 17 de mayo de 2012

Mensajes recientes

Hell-o guys

I have a servlet inside my portlet's WAR and it's working fine, the problem is that I want to get PortletPreferences from the HttpRequest object with this code:

1PortletRequest pRequest= (PortletRequest) req.getAttribute("javax.portlet.request");
2prefs= pRequest.getPreferences();

but this only return null.

I also tried using
1prefs= PortalUtil.getPreferences(req);
which also returns null


my servlet config in web.xml is this
 1<servlet>
 2    <servlet-name>enviar-comprobante-deposito</servlet-name>
 3    <servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
 4    <init-param>
 5      <param-name>servlet-class</param-name>
 6      <param-value>cl.forexchile.portlet.checkout.servlet.EnviarComprobanteDeposito</param-value>
 7    </init-param>
 8    <init-param>
 9      <param-name>sub-context</param-name>
10      <param-value>enviarComprobanteDeposito</param-value>
11    </init-param>
12    <load-on-startup>1</load-on-startup>
13  </servlet>

Is it ok? am I missing something or it's just not possible to get portlet preferences?

best regards

PS: I'm using Liferay portal 6.0.6
David H Nebinger
RE: How to get PortletPreferences from servlet
20 de diciembre de 2012 10:33
Respuesta

David H Nebinger

Ranking: Liferay Legend

Mensajes: 7238

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

This is not possible...

A portlet can be reused on one or more pages (especially instantiable portlets), meaning you can have X copies of it scattered through the portal, yet each instance has it's own preferences.

In contrast, a servlet can have only one instance. You would never know which portlet instance to use to get preferences for.

Also, an HttpServletRequest cannot be cast to a PortletRequest. You're trying to emulate what the Liferay servlet does, but that is in a different web application. Your web application has it's own request that is never going to be updated by Liferay's servlet to ever have the portlet request set as an attribute.
Milen Dyankov
RE: How to get PortletPreferences from servlet
21 de diciembre de 2012 3:18
Respuesta

Milen Dyankov

LIFERAY STAFF

Ranking: Regular Member

Mensajes: 228

Fecha de incorporación: 30 de octubre de 2012

Mensajes recientes

David's explanation is correct. This is not possible directly.
However you may want to try PortalDelegateServlet. Basically the idea behind it is that you call the portal (NOT your servlet directly) which will setup the portal context and then delegate the request to your portlet. This way you'll have access to the util classes (like PortalUtil) which I believe you can use to find you portlet and it's configuration (not sure about preferences as those are Layout based AFAIK). I used this approach a couple of years ago to allow a GWT servlet to acces the portal context. Here is the blog post which also has a links to the actual source code commits. Hope this helps.
Enzo Ferrari
RE: How to get PortletPreferences from servlet
26 de diciembre de 2012 7:48
Respuesta

Enzo Ferrari

Ranking: New Member

Mensajes: 19

Fecha de incorporación: 17 de mayo de 2012

Mensajes recientes

David H Nebinger:
This is not possible...

A portlet can be reused on one or more pages (especially instantiable portlets), meaning you can have X copies of it scattered through the portal, yet each instance has it's own preferences.

In contrast, a servlet can have only one instance. You would never know which portlet instance to use to get preferences for.

Also, an HttpServletRequest cannot be cast to a PortletRequest. You're trying to emulate what the Liferay servlet does, but that is in a different web application. Your web application has it's own request that is never going to be updated by Liferay's servlet to ever have the portlet request set as an attribute.


I thought that, by using PortalDelegateServlet I would get a portal request

Milen Dyankov:
David's explanation is correct. This is not possible directly.
However you may want to try PortalDelegateServlet. Basically the idea behind it is that you call the portal (NOT your servlet directly) which will setup the portal context and then delegate the request to your portlet. This way you'll have access to the util classes (like PortalUtil) which I believe you can use to find you portlet and it's configuration (not sure about preferences as those are Layout based AFAIK). I used this approach a couple of years ago to allow a GWT servlet to acces the portal context. Here is the blog post which also has a links to the actual source code commits. Hope this helps.


As you can see above, I'm using PortalDelegateServlet.

Anyway, I'll have to try another approach, maybe sending portletId and plid in the request to get preferences from PortletPreferencesLocalServiceUtil

thank you both for the answers
Milen Dyankov
RE: How to get PortletPreferences from servlet
26 de diciembre de 2012 8:43
Respuesta

Milen Dyankov

LIFERAY STAFF

Ranking: Regular Member

Mensajes: 228

Fecha de incorporación: 30 de octubre de 2012

Mensajes recientes

Enzo Ferrari:

As you can see above, I'm using PortalDelegateServlet.


Sorry, my bad! I didn't look carefully through your web.xml code and just assumed you have simply added your servlet there.

Enzo Ferrari:

I thought that, by using PortalDelegateServlet I would get a portal request


You actually do. The thing is (as you have already discovered yourself) it doesn't have particular page nor portlet associated with it. So basically have 2 options:
- pass those as parameters (as you have already suggested). Easy to implement but depending on your usecase you may need to secure this to not allow accessing/modifying other portlet's/instance's properties
- look for particular properties on the server side (implement custom search or iterate over all properties) based on given property name/value - Dedicated to particular portlet/instance you are interested in but more resource consuming and/or complicated to implement.
Enzo Ferrari
RE: How to get PortletPreferences from servlet
26 de diciembre de 2012 10:16
Respuesta

Enzo Ferrari

Ranking: New Member

Mensajes: 19

Fecha de incorporación: 17 de mayo de 2012

Mensajes recientes

Milen Dyankov:
Enzo Ferrari:

As you can see above, I'm using PortalDelegateServlet.


Sorry, my bad! I didn't look carefully through your web.xml code and just assumed you have simply added your servlet there.

Enzo Ferrari:

I thought that, by using PortalDelegateServlet I would get a portal request


You actually do. The thing is (as you have already discovered yourself) it doesn't have particular page nor portlet associated with it. So basically have 2 options:
- pass those as parameters (as you have already suggested). Easy to implement but depending on your usecase you may need to secure this to not allow accessing/modifying other portlet's/instance's properties
- look for particular properties on the server side (implement custom search or iterate over all properties) based on given property name/value - Dedicated to particular portlet/instance you are interested in but more resource consuming and/or complicated to implement.



I already tried to search preferences through dynamicQuery before, but I wanted to try any other approach emoticon

thanks for your answer Milen