Monday 8 August 2016

Creating a Portlet with multiple action methods Using MVC Action Command

Creating a Portlet with multiple action methods Using Action Command -:
In the previous tutorial we had created a portlet and deployed it. It was a basic sample portlet. In this tutorial we are going one step forward and we will be looking into how we can associate the actions using the Liferay MVC Command classes.



"LINK TO DOWNLOAD CODE"
In some of the cases when we are having a lot of actions to be performed the portlet or the controller class became very large. To overcome this problem liferay has introduced a concept of Command Classes to break-up the controller functionality.
Liferay has provided 3 types of command classes
  • MVCActionCommand:  These ActionCommand classes can be mapped to each of the Action URL.
  • MVCRenderCommand: These RenderCommand  hold a render method that dispatches to the appropriate JSP, by responding to render URLs.
  • MVCResourceCommand: These ResourceCommand classes are used to execute resource serving in the MVC portlet, by responding to resource URLs.
In this portlet we will be using multiple MVCActionCommand.
First of all we will create a new portlet using the previous tutorial, We will name it Student. This will create a portlet with jsp and a controller class. Something like below image.
https://lh3.googleusercontent.com/eBlQRjyQvkJx6mcDfoFeF9PO2D17R4t8PdoxSuUBqk8lrDiLufYkSMe4eeFOvpm-WyR8ly3yjmNyA8gCMTp2V4Ra7Kp9EuRiXXpSjRu7XVBhdIRsSE18AFcZd2EMWYXNV2Ow5kA1CF05jPFTiw
While using the action command the following property is very important
"javax.portlet.name= PORTLET_NAME"  
For example we had added the above property in the StudentPortlet.java The above portlet name will be used to identify the MVCActionCommand and other commands.
https://lh6.googleusercontent.com/VHHg4tjiXQU1rk8pfatGGgJGSbtFCJxn_BHZGcfR8LA5nnZ5TMcyftesGVOTuaGDLAfsFQwi4LoJkWQ8Lrd_BlXsBcJ0FQ0ZCBUbd8_f-CXFZWx-zRdEGmUq591cBmr_cAZhCx9ArGqBwKM_-g
Now let's create the GetStudentActionCommand class, this class will be called when a particular action is called.  Below is the implementation of the same. This class should implement the BaseMvcActionCommand. Also it should be having the @Component annotation so as to define the portlet name and command upon which this Action would be called.
We will now code the first ActionCommand as shown below

In the same way we will code the second controller.  Make sure that the mvc.command.name is same as given in the name attribute of ActionURL in jsp.

Now it is just a matter of calling an action method from the JSP. We can do that from the view.jsp. As shown below.

NOTE -> The property mvc.command.name should be matching to the value of the  name property in your JSP’s matching actionURL. Meaning that the name tag in the actionURL should be equalent to the property mvc.command.name in the ActionCommand Class.



And when we will click upon the generated link, we will get the following output in the liferay page.
On click it will be calling the actionUrl and actionUrl2 controller respectively.
ActionCommand1

ActionCommand2


You can download the source code from the given URL.


j\

3 comments:

  1. Hello Mohd Danish,
    I have just run this code, and then i got an error.09:13:12,870 ERROR [default task-16][render_portlet_jsp:77] null
    java.lang.ClassCastException: com.sun.el.ExpressionFactoryImpl cannot be cast to javax.el.ExpressionFactory
    My environment: Liferay 7 GA3, Wildfly 10.
    Do you have any idea about how to resolve that error?
    Many thanks

    ReplyDelete
  2. Looks like you are having multiple jars, can you please check you are having a single Jar file (el-api.jar) across the Environment?

    ReplyDelete
  3. Hi,
    Many thanks for your example!
    While reading Liferay docs I couldn't undertend what the differnece between MVCActionCommand and MVCRenderCommand?
    All these commands can return a new view (jsp).
    In which particular case we heed use MVCActionCommand and when MVCRenderCommand.

    Thank you in advance!

    ReplyDelete

 

Copyright @ 2016 LiferayRevisited.