Inter Portlet Communication can be done by many ways like by using events,public render parameters and Portlet Session. Today we discuss about IPC by using Portlet Sessions. Here we set some value in one portlet and get value in other Portlet.
We take a simple scenario where user can select food type from one portlet and second portlet shows food items on the basis of first portlet. This is a Sender, Receiver kind of scenario.Here we create two project and each project contain one portlet.
So lets start this step by step:-
For Sender
Step 1:-Create Liferay ProjectInside eclipse IDE Click on File->New->Liferay plugin project and give the project name as Session_Sender and click finish.
Step 2:-Create MVC Portlet inside projectRight Click on your project ie Session_Sender 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 blog on Portlet Configuration Page in Liferay.
Step 3:-Change liferay-portlet.xmlOpen liferay-display.xml and set private session attribute to false so that attribute set in session available to different wars:-
<private-session-attributes>false</private-session-attributes>Also we are not using AUI so set required namespace parameter to false:-
<requires-namespaced-parameters> false</requires-namespaced-parameters>Now your liferay-portlet.xml become:-
Step 4:-Change view.jspOpen view.jsp and paste this content:-
Explanation:-Creating a simple form with two radio button on the submittion of form processFood method is called.
Step 5:-Change the ControllerOpen your java class and paste this:-
Explanation:-Here we fetch the value from form and set in Session Object with Application Scope so that it is available to whole application.We have one more Scope called Portlet scope.
For Receiver
Step 1:-Create Liferay ProjectInside eclipse IDE Click on File->New->Liferay plugin project and give the project name as Session_Receiver and click finish.
Step 2:-Create MVC Portlet inside projectRight Click on your project ie Session_Sender 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 blog on Portlet Configuration Page in Liferay.
Step 3:-Change liferay-portlet.xmlOpen liferay-display.xml and set private session attribute to false as in Sender.
Step 4:-Change your ControllerOpen your java class and paste this:-
Explanation:-Here we fetch the value from session that is set by the Sender and again set in renderRequest so that it is available in view.jsp.
Step 5:-Change view.jspOpen view.jsp of receiver as:-
Explanation:-Here we get the value that is set in Controller and on the basis of condition show different messages.
Note:- We are using jstl tage here so provide entry in liferay-plugin-package.property file as:-
For Output
Now deploy both sender and receiver portlet and add to same page or different page. I am adding Sender on one page and Receiver on other.
Here i am adding Sender on Welcome page and select Vegetarian and click Submit:-
The Receiver Portlet is on about page:-
Now again go to and select Non- Vegetarian and click submit and see the Receiver as:-
Service builder in Liferay provide a easy way to created services. We just need to provide the table structure in a xml file and all the CRUD methods generate automatically. For custom methods you may create Custom Sql and Finder Methods.
Today we create a basic services with just single table and see how to enter data in the table.
So lets start this step by step:-Step 1:-Create Liferay ProjectInside eclipse IDE Click on File->New->Liferay plugin project and give the project name as service_demo and click finish.
Step 2:-Create MVC Portlet inside projectRight Click on your project ie service_demo 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 blog on Portlet Configuration Page in Liferay.
Step 3:-Create Service.xmlRight Click on your project ie service_demo and click on New than click on Liferay Service Builder. Provide the package path, namespaces and author
and Click Finish. This will create a service.xml inside WEB-INF.
Step 4:-Create Entity in Service.xmlOpen Service.xml and Click overview and then click on entity.By using the "+" button you may enter as many entities as you want each entity represent one table in database.
Here we use Employee as name of the entity so all services is created with Employee as prefix.
But we want the table name in database is employee_data .So Click on Employee on Left and fill Table as employee_data
So our table is created with name employee_data and our entity name is Employee.
Step 5:-Create columns in tableClick on columns on Left and by using the "+" button add columns
Here we check eid Primary checkbox so that eid become primary key for the table.
Now we want the primary key is auto incremented. So select eid from left, this will open property page for eid enter Id type to increment .
We have other options for Id type like sequence, identity, class but i am using Mysql so use increment.
Now all the columns in database will created with eid,name,phone ,age but i want that salary is created in database as EMP_SALARY so from Left click on salary and change Db name as:-
When you click on source you will see the service.xml as:-
We can directly create service.xml without using GUI. So you can directly copy paste service.xml and skip step 4 and 5.
Step 6:-Build ServicesClick on overview in service.xml and click on Build service icon as:-
After build service is completed you can check all class are created in src folder.
Step 7:-Call the services Open your controller of the portlet ie java class and paste this:-
Explanation:-Here in doView method we use CounterLocalServiceUtil to generate primary key ie eid because we take eid as autoincremented.
Step 8:-Check OutputOpen your database you can see table as:-
Check data for that table
Project Structure
Hope this will Help....
You can Download Source code from Service Builder in Liferay.
In the next blog Service Builder in Detail we will see each and every tag of service.xml in detail.
Related Post:-
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 ProjectInside 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 projectRight 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 JspOpen 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 ClassOpen com.demo package a create a java class with name ConfigurationAction that extends DefaultConfigurationActionNow 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 tabProvide 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.jspOpen 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 OutputNow 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 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 ProjectInside 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 projectRight 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 ClassOpen 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 jspOpen 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 jspOpen 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.jspOpen 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 OutputNow 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:-
We already know each portlet have three modes view, edit and help. In this blog we see Edit mode in detail before reading this blog you must know about Portlet Modes in Liferay.In this blog we use a simple form in edit mode where user can set his preferences that he is Vegetarian or non-vegetarian and on this basis we provide a menu of food items.
So lets start this step by step:-Step 1:-Create Liferay ProjectInside eclipse IDE Click on File->New->Liferay plugin project and give the project name as edit-mode and click finish.
Step 2:-Create MVC Portlet inside projectRight Click on your project ie edit-mode and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click next.
In the next screen check Edit in Portlet modes this will provide the necessary entry in portlet.xml and also create edit.jsp . Now Click Finish.
Step 3:-Modify your edit.jspOpen edit.jsp and paste this content:-
Explanation:-Here we create a simple form which contain two radio button .On the submission of form processFood method is called inside the controller.
Note:- As we are not using AUI tags so add required name space parameter is false inside liferay-portlet.xml. For more details you may refer Namespaces parameter in Liferay.
Step 4:-Modify your ControllerOpen your java class and paste this content:-
Explanation:-1)In processFood method we get the foodType value that is submitted by the form in edit.jsp and than set this value in PortletPreferences and finally send the response to view mode.2)In doView method we get the foodType value from PortletPreferences and set in renderRequest so that it is available on view.jsp.3)In doView we also check the value of foodType because each time portlet load doView is called .So first time when edit Mode is not called foodType is null .
Step 5:-Modify your view.jspOpen your view.jsp and paste this content:-
Explanation:-Here we are get and set foodType in a variable food and check the value and on the basis of value we display the messages.
Note:-As you see we use jstl tags in view.jsp so we have to include jstl jars inside liferay-plugin-package.properties as:-
Step 6:-Check OutputDeploy the portlet and add on page .Now click on gear icon and click Preferences:-
Now select vegetarian and click submit
You will see output as:-
Now again click on gear icon than preferences and select Non-Vegetarian and click submit.You will see output as:-
Hope this will Help....
You can Download Source code from Edit Mode in Liferay.
Related Post:-