Thursday, 30 April 2015

How to create multiple actions in Liferay MVC

There are 4 simple steps to create Multiple actions in Liferay MVC Portlet

1) Create multiple liferay actions inside JSP
2) Create corresponding action method inside controller
3) Create buttons inside JSP with different action to be performed
4) Write below script to change the action method on form submit based on the student action performed

Here are the detailed level steps

1) Create multiple liferay actions inside JSP

<portlet:actionURL name="saveStudentDetails" var="saveStudentDetails" />
<portlet:actionURL var="getStudentDetails" name="getStudentDetails" />    


2) Create corresponding action method inside controller

public void saveStudentDetails(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {

System.out.println("Inside save student details");
//Your custom logic to save student details starts
}


public void getStudentDetails(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {

System.out.println("Inside get student details");
//Your custom logic to fetch student details starts
}


3) Create buttons inside JSP with different action to be performed

<input type="button" name="retrieveStudentDetails" id="retrieveStudentDetails" onclick='getStudentDetail()' value="Get Student Details" /> &nbsp;
<input type="button" name="saveStudentDetail" id="saveStudentDetail" onclick='saveStudentDetail()' value="Save Student Details" /> <br/>


4) Write below script to change the action method on form submit based on the student action performed

<script type="text/javascript">

    //Below method to be called if user clicks on the "retrieveStudentDetails" button
    function getStudentDetail() {
    document.fm.action='<%=getStudentDetails%>';
    document.fm.submit();
    }
    
    //Below method to be called if user click on the save student details
    function saveStudentDetail() {
    document.fm.action='<%=saveStudentDetails%>';
    document.fm.submit();
    }

</script>

Cheers!
Henal Saraiya
(Senior Consultant)
CIGNEX Datamatics

No comments:

Post a Comment