Thursday, September 24, 2015

Portlet Preferences in Liferay


Today we will discuss about Portlet Preferences in Liferay. Lets discuss some basic things about Portlet Preferences:-

  • Portlet Preferences are properties for storing basic portlet configuration data.
  • Portlet Preferences are key value pair that are stored by the portal.
  • These preferences are available via PortletPreferences interface.
  • The values are stored in portletpreferences table.


We can set preferences by two ways:-
  1)By using portlet.xml.
  2)By using PortletPreferences interface.

You can read more about PortletPreferences here.

So lets start this step by step:-




Step 1:-Create Liferay Project and Portlet
Create a Liferay Project in eclipse and then create a portlet in the Project.For theses you may refer Step 1 and Step 2 of my previous blog Here .

Step 2:-Change portlet.xml
Open your portlet.xml and after </portlet-info> paste this:-
     
  <portlet-preferences> 
        <preference>
            <name>State</name>
            <value>Delhi</value>
         </preference>
    </portlet-preferences>

Explanation:-

  • Here we create a Key State and set a value Delhi to it.
  • We can also assign multiple values to a single key which return  array when we get the values.
  • We can use as many <preference></preference> tags for each  key-value pairs.

Step 3:-Change the Controller
Open your java file and paste this:-

Explanation:-
Here we are using PortletPreferences object and add another key-value pair and by using store() we commit this. For storing key-value in database store() is compulsory.

Step 4:-Change the view
Open your view.jsp and paste this

Explanation:-
Here we just get the values from PortletPreferences object.

Step 5:-Check the Output
Now deploy your portlet and check the output as:-



Note :- You can also change the values that are set in portlet.xml by using this code:-

                String state = preferences.getValue("State", "");
                preferences.setValue("State", "UP");
                preferences.store();



Database Tables


Note :- As soon as you remove the portlet from page this entry is also removed from table portletpreferences.






Hope this will Help....

You can Download Source code from  Portlet Preferences in Liferay.

Related Post:-

No comments:

Post a Comment

Total Pageviews

Number Of Unique Visitor

Free counters!