Unique Way to Save Portlet Preferences in Liferay 6.1

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.
Define your AUI element with below syntax

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

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 %>" />

 

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", StringPool.BLANK);
 
 

 

Hope this will help ..!!

Jitendra Rajput

http://itsliferay.blogspot.in/

4
Blogs
Hi Jitendra,

The post you have written is a code saver. This is another best use of alloy aui which we can use. Now i do not need to store form elements values in preferences and create another java file in portlet.

Thnxx for sharing this information with us.

I tried above code and would like to discuss one issue, i think the correct method is below one for getting stored values:
preferences.getValue("showFeedTitle",StringPool.BLANK);

And i would like to ask, in which cases we should use GetterUtil:
GetterUtil.getString(preferences.getValue("showFeedTitle",StringPool.BLANK));
Thank you vaibhav , I have corrected the article .

GetterUtil.getString(preferences.getValue("showFeedTitle",StringPool.­BLANK)); i guess this Utils are basically used to avoid null pointer exception and for internal type cast.

But if you retrieve preferences like below
preferences.getValue("showFeedTitle", StringPool.BLANK);
then no need to user GetterUtil because if the value of showFeedTitle title is null it will be loaded with blank value as specified in second argument.

Jitendra Rajput
http://itsliferay.blogspot.in/
HI Jitendra, Can you post a complete example for this? I don't undertand very well, for example...

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

What is de action in this form? I put some like this:

<liferay-portlet:actionURL name="mvcPath" portletConfiguration="true" var="mvcPath" ></liferay-portlet:actionURL>

<aui:form action="<%=mvcPath %>" method="post" name="<portlet:namespace/>">
<aui:input name="preferences--showFeedTitle--" type="text" />
<input type="submit" value="click me"/>
</aui:form>

but I have an error

--> Hola mundo
21:02:05,320 ERROR [http-bio-8080-exec-111][PortletServlet:115] javax.portlet.PortletException: processAction method not implemented

this is my action
public class HelloWorldPortlet extends GenericPortlet {
public void processAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {

System.out.println("-->Hola mundo");

super.processAction(actionRequest, actionResponse);
}
}

thanks, Alex.
Hi .. For your information i have posted one sample JSP..

https://gist.github.com/jitendra-rajput/11041566

Hope this help .!!

Jitendra
http://itsliferay.blogspot.in/