Foren
How to get PortletPreferences from servlet
Enzo Ferrari, geändert vor 11 Jahren.
How to get PortletPreferences from servlet
New Member Beiträge: 20 Beitrittsdatum: 17.05.12 Neueste 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:
but this only return null.
I also tried using
my servlet config in web.xml is this
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
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:
PortletRequest pRequest= (PortletRequest) req.getAttribute("javax.portlet.request");
prefs= pRequest.getPreferences();
but this only return null.
I also tried using
prefs= PortalUtil.getPreferences(req);
which also returns nullmy servlet config in web.xml is this
<servlet>
<servlet-name>enviar-comprobante-deposito</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>cl.forexchile.portlet.checkout.servlet.EnviarComprobanteDeposito</param-value>
</init-param>
<init-param>
<param-name>sub-context</param-name>
<param-value>enviarComprobanteDeposito</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</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, geändert vor 11 Jahren.
RE: How to get PortletPreferences from servlet
Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste 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.
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, geändert vor 11 Jahren.
RE: How to get PortletPreferences from servlet
Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste 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.
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, geändert vor 11 Jahren.
RE: How to get PortletPreferences from servlet
New Member Beiträge: 20 Beitrittsdatum: 17.05.12 Neueste BeiträgeDavid 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, geändert vor 11 Jahren.
RE: How to get PortletPreferences from servlet (Antwort)
Expert Beiträge: 310 Beitrittsdatum: 30.10.12 Neueste BeiträgeEnzo 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, geändert vor 11 Jahren.
RE: How to get PortletPreferences from servlet
New Member Beiträge: 20 Beitrittsdatum: 17.05.12 Neueste BeiträgeMilen 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
thanks for your answer Milen