Thursday, November 2, 2017

Multiple ActionURL Call on Click of Buttons in Liferay


We already know how to submit a form in Liferay. Normally we create a Form with a submit button and in Form action we pass actionURL .

When we click the submit button the form is submitted and call the process action method. But sometimes we need to call various action methods on clicking of simple buttons. So today we will discuss this in detail.

So lets start this Step by Step:-

Step 1:-Create a project and portlet
Create a project and then create a portlet in it .Here my project name is multisubmit-portlet and portlet name is Demo.






Step 2:-Change the jsp
Change your view.jsp as:-

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<portlet:defineObjects />
<c:set var="pns" scope="request"><portlet:namespace /></c:set>
<portlet:actionURL var="firstURL" name="first"/>
<portlet:actionURL var="secondURL" name="second"/>
<aui:form name="multiSubmitForm">
<aui:button id="${pns}firstButton" type="submit" value="First" />
<aui:button id="${pns}secondButton" type="submit" value="Second" />
</aui:form>
<h4>${message}</h4>
<aui:script>
AUI().use('aui-node', function(A) {
var firstURL = '<%=firstURL.toString() %>';
var firstButton = A.one('#${pns}firstButton');
var secondURL = '<%=secondURL.toString() %>';
var secondButton = A.one('#${pns}secondButton');
firstButton.on('click', function(event){
document.<portlet:namespace/>multiSubmitForm.action = firstURL;
});
secondButton.on('click', function(event){
document.<portlet:namespace/>multiSubmitForm.action = secondURL;
});
});
</aui:script>
view raw view.jsp hosted with ❤ by GitHub


Explanation:-
1)In the view.jsp first we create two actionURLs (firstURL and SecondURL).
2)Then we create two aui buttons and handle the click event by using aui.
3)${message} is shown when we click on a button . This message is set in our controller.
4)Here we use c tag so add the jstl jars in liferay-plugin-package.properties.


Step 3:-Change the Controller
Now change your Demo.java:-

Demo.java
package com.demo;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
public final void first(final ActionRequest request, final ActionResponse response) {
System.out.println("First");
request.setAttribute("message", "First Button is Called.....");
}
public final void second(final ActionRequest request, final ActionResponse response) {
System.out.println("Second");
request.setAttribute("message", "Second Button is Called.....");
}
}
view raw Demo.java hosted with ❤ by GitHub


Explanation:-
1)Here we handle the request when first button is clicked first method is called and when second button is clicked second method is called.
2)Here we also set the message in request that can be used on our jsp.


Step 4:-Check the Output
Now deploy the portlet and check the output:-


When we click on First:-


When we click on second


No comments:

Post a Comment

Total Pageviews

1039674

Number Of Unique Visitor

Free counters!