Save Portlet Preferences in Liferay 6.1

1 comment
With Liferay 6.1 if you are using configuration mode and if you want to store form elements values in preferences then this post will definitely help you.

You can store form elements value in preferences with less efforts (as compare to version below Liferay 6.1).

You just need to follow few syntax to achieve this functionality. Lets go step by step.

Edit :Create hidden field with the name command so that Controller will automatically understand what you are trying to do.

<input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />

Define your AUI element with below syntax

<aui:input name="preferences--showFeedTitle--" type="checkbox" value="<%= showFeedTitle %>" />


It should follow pattern like preferences--paramName-- .

And make sure you use below Configuration class in your liferay-portlet.xml

<configuration-action-class>com.liferay.portal.kernel.portlet.DefaultConfigurationAction</configuration-action-class>

By using this way we dont need to write any code to store form element value into preferences . DefaultConfigurationAction class will detect form element with specified syntax and store the same in preference for you.

In case if you want to retrieve value from preference then directly use

preferences.getValue("showFeedTitle")
// No need to use that syntax to get value  


You can check sample jsp for the same on this link

You can also visit Liferay for same thing . Following is my community blog link. Unique way to save portlet preferences

Next PostNewer Post Previous PostOlder Post Home

1 comment:

  1. Hi, thank you excellent guide. I have a question for you. What if I defined a preferences with multiple values? Is there any alloy command to update the preferences? I have some preferences like this:


    MultipleValues
    Number 2
    Number 3


    in the config jsp I can access them like this:

    <%
    PortletPreferences preferences = renderRequest.getPreferences();
    String portletResource = ParamUtil.getString(request, "portletResource");

    PortletPreferences prefs =
    PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);

    String [] mul = prefs.getValues("MultipleValues",
    new String []{"", ""});
    %>




    but when I click save it will be saved in a different single preference value, not an array.

    Thank you in advance.

    ReplyDelete