Showing posts with label Portlet Prefrences. Show all posts
Showing posts with label Portlet Prefrences. Show all posts

Portlet Configuration Page in Liferay using Custom Class


We already discuss about Portlet Configuration page using DefaultConfiguration class provide by liferay. In this blog we create the same application but by using our own custom class.Here we are created a simple table where user can configure number of columns at run time. ie Show/Hide columns at run time.

So lets start this step by step:-

Step 1:-Create Liferay Project
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as configure-table and click finish.






Step 2:-Create MVC Portlet inside project
Right Click on your project ie configure-table and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.

Note:-You can check the snapshots in my previous blog on Portlet Configuration Page in Liferay.


Step 3:-Create Configuration Jsp
Open html/demo package and create a jsp with name configuration.jsp and paste this content:-


Explanation:-
Here we just create a simple form with 4 check box. We create a actionUrl with portletConfiguration="true" so that it will call Configuration class processAction method and not of portlet processAction method.

Step 4:-Create Configuration Class
Open com.demo package a create a java class with name ConfigurationAction that extends DefaultConfigurationAction
Now override the processAction() and render() as:-


Explanation:-
1)Here we just fetch the values and set in Preferences.
2)Render method just redirect the flow to configuration.jsp.
3)The last line in Action Method refresh the page so that when we go back to page new result is shown.

Step 5:-Enable SetUp tab
Provide a entry in liferay-portlet.xml so that set up tab is enable and when we click on set up tab our Configuration Class is Called. Open liferay-portlet.xml and after <icon> tag paste this

<configuration-action-class>
       com.demo.ConfigurationAction
</configuration-action-class>


Step 6:-Change the view.jsp
Open view.jsp and paste this content:-


Explanation:-
Here we just fetch the values from preferences and show the column if value is true. Here we use jstl tags so provide the jstl entries in liferay-plugin-package.properties file as :-




Step 7:-See the Output
Now deploy the portlet and add to page and see the output as:



Now Select the fields you want to show


Now go to the page
Now you can again go to configuration->set up and again select/deselect the number of columns.

Project Structure








Hope this will Help....

You can Download Source code from  Portlet Configuration Page.

Related Post:-

Portlet Configuration Page in Liferay using DefaultConfigurationAction


Portlet Configuration page in Liferay is a very handy feature by which we can store portlet related configuration at run time. Configuration page in Liferay uses preferences to store the configuration. This can be achieved by two ways:-

1)By using DefaultConfigurationAction Class.
2)By using own class that extends DefaultConfigurationAction .

In this blog we will use the first approach.Here we are created a simple table where user can configure number of columns at run time. ie Show/Hide columns at run time.

So lets start this step by step:-

Step 1:-Create Liferay Project
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as configure-table and click finish.





Step 2:-Create MVC Portlet inside project
Right Click on your project ie configure-table and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.



Step 3:-Provide Configuraion Action Class
Open liferay-portlet.xml file and provide the entry of configuration action class as:-

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

Now your liferay-portlet.xml become:-


Note:- The DefaultConfigurationAction class provide all the all the functionality to store the preferences . You can see the source code for the same.

Step 4:-Provide Configuration jsp
Open html/demo and create a jsp with name configuration.jsp and paste this content:-



Explanation:-
1)We create a actionUrl with portletConfiguration="true" .
If it is false than this behave as normal actionUrl and invoke processAction() of portlet.And not invoke the action method of DefaultConfigurationAction class.

2)We give name of input field as preferences--propertyName--  this is must so that these are set in preferences.

3)We use a hidden field with name cmd and value update so that as soon as form submit page refresh automatically.

Note:- You can see the source code of DefaultConfigurationAction for the same.


Step 5:-Provide entry of Configuration jsp
Open portlet.xml and provide the entry of jsp created in previous step so that when we click set up the jsp open for configuration .

  <init-param>
<name>config-template</name>
<value>/html/demo/configuration.jsp</value>
</init-param>

Here the name must be config-template.


Step 6:-Change view.jsp
Open view.jsp and paste this:-


Explanation:-
Here we just fetch the preferences and on the basis of condition show the columns.

Note:-As we use the jstl library so add jstl jars in 
liferay-plugin-package.property file as:-




Step 7:-See the Output
Now deploy the portlet and add to page and see the output as:



Now Select the fields you want to show


Now go to the page 
Now you can again go to configuration->set up and again select/deselect the number of columns.

Project Structure







Hope this will Help....

You can Download Source code from  Portlet Configuration Page.

In the next blog we will do the same thing with our own custom class. You can see my next blog on Configuration Page using own Custom Class for this.



Related Post:-

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:-