Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Enzo Ferrari
How to get PortletPreferences from servlet
26. Dezember 2012 07:29
Antwort

Enzo Ferrari

Rang: New Member

Nachrichten: 19

Eintrittsdatum: 17. Mai 2012

Neue Beiträge

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. Dezember 2012 10:33
Antwort

David H Nebinger

Community Moderator

Rang: Liferay Legend

Nachrichten: 8956

Eintrittsdatum: 1. September 2006

Neue Beiträge

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. Dezember 2012 03:18
Antwort

Milen Dyankov

LIFERAY STAFF

Rang: Regular Member

Nachrichten: 232

Eintrittsdatum: 30. Oktober 2012

Neue Beiträge

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. Dezember 2012 07:48
Antwort

Enzo Ferrari

Rang: New Member

Nachrichten: 19

Eintrittsdatum: 17. Mai 2012

Neue Beiträge

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. Dezember 2012 08:43
Antwort

Milen Dyankov

LIFERAY STAFF

Rang: Regular Member

Nachrichten: 232

Eintrittsdatum: 30. Oktober 2012

Neue Beiträge

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. Dezember 2012 10:16
Antwort

Enzo Ferrari

Rang: New Member

Nachrichten: 19

Eintrittsdatum: 17. Mai 2012

Neue Beiträge

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