Foren

Not getting PortletPreferences

Joona Vaurio, geändert vor 15 Jahren.

Not getting PortletPreferences

New Member Beiträge: 21 Beitrittsdatum: 22.07.08 Neueste Beiträge
Hello.

I've faced another problem in my development. I am not getting the preferences for a portlet set in portlet.xml file. Here's a demonstration:

portlet.xml
...
<portlet-preferences>
	<preference>
		<name>title</name>
		<value>my title</value>
	</preference>
</portlet-preferences>
...

JSPPortlet.java

...
public void doView(RenderRequest req, RenderResponse res)
	throws IOException, PortletException {

	PortletPreferences prefs = req.getPreferences();
	String title = prefs.getValue("title", "default");
	req.setAttribute("title", title);
	include(viewJSP, req, res);
}
...


view.jsp
...
&lt;%=  request.getAttribute("title") %&gt;
...


I've looked in to some portlet guides in the web and this should work as it is, but the result is always the default value. Why? The portlet is a regular JSP portlet, that has been created with plugins SDK.
thumbnail
Jonatan Oyola, geändert vor 15 Jahren.

RE: Not getting PortletPreferences

Regular Member Beiträge: 193 Beitrittsdatum: 06.06.07 Neueste Beiträge
Try with:


PortletPreferences prefs = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
	prefs = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}
Tim J W, geändert vor 15 Jahren.

RE: Not getting PortletPreferences

New Member Beiträge: 12 Beitrittsdatum: 27.08.08 Neueste Beiträge
I'm having the exact same problem. I set the preferences in my portlet.xml file, then try to call it inside my doView method like this:

portlet.xml:

<portlet-preferences>
	<preference>
		<name>myHost</name>
		<value>123.123.123.123</value>
  </preference>
  <preference>
		<name>myUsername</name>
		<value>foo</value>
  </preference>
  <preference>
		<name>myPassword</name>
		<value>bar</value>
  </preference>
</portlet-preferences>


inside my doView method in my java file:

import javax.portlet.PortletPreferences;

...

PortletPreferences prefs = req.getPreferences();
String host = prefs.getValue("myHost", "defaultValue");
String username = prefs.getValue("myUsername", "defaultValue");
String password = prefs.getValue("myPassword", "defaultValue");
System.out.println(host);
System.out.println(username);
System.out.println(password);


However, instead of the values stored in portlet.xml, I get "defaultValue" for all 3. What gives?
thumbnail
Manish Kumar Gupta, geändert vor 15 Jahren.

RE: Not getting PortletPreferences

Liferay Master Beiträge: 535 Beitrittsdatum: 16.05.08 Neueste Beiträge
are you working on trunk or on some released version??
Tim J W, geändert vor 15 Jahren.

RE: Not getting PortletPreferences

New Member Beiträge: 12 Beitrittsdatum: 27.08.08 Neueste Beiträge
I'm working against the liferay tomcat bundle version 5.1.1. I'm not using source.

Is that why it's not working?
Tim J W, geändert vor 15 Jahren.

RE: Not getting PortletPreferences

New Member Beiträge: 12 Beitrittsdatum: 27.08.08 Neueste Beiträge
Anyone? Any ideas?
thumbnail
Ray Augé, geändert vor 15 Jahren.

Re: [Liferay Forums][3. Development] RE: Not getting PortletPreferences

Liferay Legend Beiträge: 1197 Beitrittsdatum: 08.02.05 Neueste Beiträge
Tim are you working with an instanceable or non-instanceable portlet?

i.e. do you have [tt]<instanceable>true</instanceable>[/tt] in your
[tt]liferay-portlet.xml[/tt] file?

If so, can you go to another page and add the portlet to this page? Do
you get the preference now?
If not, go to another Community where the portlet was never added and
add it. Do you get the preference now?

New preferences are only loaded for a portlet the first time that
portlet is initialized in a new context... further changes to the
default preferences will only affect newly initialized portlets. A
portlet that is not instanceable has community wide preferences... so
going to a new page does not give you new preferences by default, though
you can configure this behaviour.

HTH!
cee bee, geändert vor 13 Jahren.

RE: Not getting PortletPreferences

New Member Beiträge: 3 Beitrittsdatum: 05.11.10 Neueste Beiträge
But you redeployed the portlet completly during development ?

You have to redeploy and add it to the page again, so liferay rereads the portlet.xml again ...
Vasya Patricov, geändert vor 12 Jahren.

RE: Not getting PortletPreferences

New Member Beiträge: 9 Beitrittsdatum: 11.01.11 Neueste Beiträge
I know answer for this question. All writed above is fake emoticon
If you want to get default portlet properties, that stored in portlet.xml use this code:
Map<string, string[]> defaultPreferencesMap = PortletPreferencesLocalServiceUtil.getDefaultPreferences(
    companyId,
    portletName
).getMap()</string,>

If you want get properties, for particular portlet, stored in PortletProperties table use this:
request.getPreferences()
thumbnail
Nicolas Forney, geändert vor 12 Jahren.

RE: Not getting PortletPreferences

Junior Member Beiträge: 78 Beitrittsdatum: 23.05.11 Neueste Beiträge
Hi,

You are right, to get access to portlet static preferences it's necessary to use the PortletPreferencesLocalServiceUtil.getDefaultPreferences(companyId, portletName).

If only API were better commented, it will be easier to find those kind of information without searching for hours into the source code....

Thank you Vasya.