Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Joona Vaurio
Not getting PortletPreferences
September 2, 2008 12:55 AM
Answer

Joona Vaurio

Rank: New Member

Posts: 23

Join Date: July 22, 2008

Recent Posts

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
1...
2<portlet-preferences>
3    <preference>
4        <name>title</name>
5        <value>my title</value>
6    </preference>
7</portlet-preferences>
8...

JSPPortlet.java
 1
 2...
 3public void doView(RenderRequest req, RenderResponse res)
 4    throws IOException, PortletException {
 5
 6    PortletPreferences prefs = req.getPreferences();
 7    String title = prefs.getValue("title", "default");
 8    req.setAttribute("title", title);
 9    include(viewJSP, req, res);
10}
11...


view.jsp
1...
2<%=  request.getAttribute("title") %>
3...


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.
Jonatan Oyola
RE: Not getting PortletPreferences
September 2, 2008 3:20 AM
Answer

Jonatan Oyola

Rank: Regular Member

Posts: 183

Join Date: June 6, 2007

Recent Posts

Try with:

1
2PortletPreferences prefs = renderRequest.getPreferences();
3String portletResource = ParamUtil.getString(request, "portletResource");
4if (Validator.isNotNull(portletResource)) {
5    prefs = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
6}
Tim J W
RE: Not getting PortletPreferences
September 11, 2008 12:17 PM
Answer

Tim J W

Rank: New Member

Posts: 12

Join Date: August 27, 2008

Recent Posts

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:
 1
 2<portlet-preferences>
 3    <preference>
 4        <name>myHost</name>
 5        <value>123.123.123.123</value>
 6  </preference>
 7  <preference>
 8        <name>myUsername</name>
 9        <value>foo</value>
10  </preference>
11  <preference>
12        <name>myPassword</name>
13        <value>bar</value>
14  </preference>
15</portlet-preferences>


inside my doView method in my java file:
 1
 2import javax.portlet.PortletPreferences;
 3
 4...
 5
 6PortletPreferences prefs = req.getPreferences();
 7String host = prefs.getValue("myHost", "defaultValue");
 8String username = prefs.getValue("myUsername", "defaultValue");
 9String password = prefs.getValue("myPassword", "defaultValue");
10System.out.println(host);
11System.out.println(username);
12System.out.println(password);


However, instead of the values stored in portlet.xml, I get "defaultValue" for all 3. What gives?
Manish Kumar Gupta
RE: Not getting PortletPreferences
September 11, 2008 6:13 PM
Answer

Manish Kumar Gupta

LIFERAY STAFF

Rank: Liferay Master

Posts: 535

Join Date: May 15, 2008

Recent Posts

are you working on trunk or on some released version??
Tim J W
RE: Not getting PortletPreferences
September 12, 2008 12:13 AM
Answer

Tim J W

Rank: New Member

Posts: 12

Join Date: August 27, 2008

Recent Posts

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
RE: Not getting PortletPreferences
September 14, 2008 7:16 PM
Answer

Tim J W

Rank: New Member

Posts: 12

Join Date: August 27, 2008

Recent Posts

Anyone? Any ideas?
Ray Augé
Re: [Liferay Forums][3. Development] RE: Not getting PortletPreferences
September 14, 2008 8:36 PM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1171

Join Date: February 7, 2005

Recent Posts

Tim are you working with an instanceable or non-instanceable portlet?

i.e. do you have <instanceable>true</instanceable> in your
liferay-portlet.xml 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
RE: Not getting PortletPreferences
December 14, 2010 7:51 AM
Answer

cee bee

Rank: New Member

Posts: 3

Join Date: November 5, 2010

Recent Posts

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
RE: Not getting PortletPreferences
January 19, 2012 12:47 AM
Answer

Vasya Patricov

Rank: New Member

Posts: 9

Join Date: January 10, 2011

Recent Posts

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:
1Map<String, String[]> defaultPreferencesMap = PortletPreferencesLocalServiceUtil.getDefaultPreferences(
2    companyId,
3    portletName
4).getMap()

If you want get properties, for particular portlet, stored in PortletProperties table use this:
1request.getPreferences()
Nicolas Forney
RE: Not getting PortletPreferences
March 3, 2012 4:58 PM
Answer

Nicolas Forney

Rank: Junior Member

Posts: 78

Join Date: May 23, 2011

Recent Posts

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.