Tuesday, October 4, 2016

Developing a MVCPortlet with Multiple Actions in Liferay

Today we will discuss how to develop a MVCPortlet with multiple action methods in Liferay. Here we create a portlet that contain two action methods with simple message.So lets start this step by step:-


Step 1:-Create Liferay Plugin Project 
Click on File  -->New -->Liferay plugin project. A screen will appear



Provide the Project name and Select Plugin Type as Portlet and click finish.

Step 2:-Create Portlet in Project
In package explorer Right click on project --> New --> Liferay Portlet


provide class name, package and select SuperClass as MVCPortlet.



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

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<!-- Action URLs -->
<portlet:actionURL var="firstActionURL" name="firstMethod" />
<portlet:actionURL var="secondActionURL" name="secondMethod" />
<h4>First Form</h4>
<form action="<%=firstActionURL%>" method="POST">
First :-<input type="text" name='<portlet:namespace/>first'>
<input type="submit" value="SUBMIT">
</form>
<h4>Second Form</h4>
<form action="<%=secondActionURL%>" method="POST">
Second :-<input type="text" name='<portlet:namespace/>second'>
<input type="submit" value="SUBMIT">
</form>
view raw view.jsp hosted with ❤ by GitHub

Explanation:-
Here we simply create two actionURLs and two forms. Each actionURL is associated with a form.




Step 4:-Change the Controller
Open your java class and paste this:-

Demo.java
package com.demo;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
public void firstMethod(ActionRequest request,ActionResponse response) throws IOException, PortletException {
String first = ParamUtil.getString(request, "first");
System.out.println("First=>"+first);
}
public void secondMethod(ActionRequest request,ActionResponse response) throws IOException, PortletException {
String second = ParamUtil.getString(request, "second");
System.out.println("second=>"+second);
}
}
view raw Demo.java hosted with ❤ by GitHub

Explanation:-
Here we create two method each method handle one form. The name of the methods must be same in the name variable in actionURL creation. Ex:-
<portlet:actionURL var="firstActionURL"  name="firstMethod" />
so first form is handle by firstMethod.





Step 5:-Check the Output
Deploy the portlet and add this to a page :-
On the Console:-
First=>Aditya First

On the Console:-
Second=>Second Test


Hope this will Help....

You can Download Source code from  Multiple Action Method in MVCPortlet. 

Related Post:-

No comments:

Post a Comment

Total Pageviews

1039181

Number Of Unique Visitor

Free counters!