Liferay 7 Portlet Commands Example


Liferay 7 Portlet Commands Example

Liferay 7 Portlet Commands Example tutorial helps developers in creating commands rather writing up all action,resource methods in Portlet controller. It will helpful and easy in code maintenance if we split the action methods into different classes.

The below are available commands in Liferay 7:

  • MVCActionCommand
  • MVCRenderCommand
  • MVCResourceCommand

MVCRenderCommand

MVC Render Command  an interface to handle the render phase of the portlet and can only be used for MVC portlets that extends MVCPortlet. Liferay 6 versions uses mostly mvcPath or custom command as init parameter to navigate to particular JSP, but in Liferay 7 it allows to create multiple commands that implement MVCRenderCommand class. In Leave application, let’s take below scenarios:

  • EditLeave – Create View Command EditViewLeaveCommand and return editLeave.jsp. In the command class, inject leaveLocalService and set the corresponding bean info rather writing up in editLeave.jsp
  • ViewLeaveInfo  – Create ViewLeaveInfoCommand and return leaveinfo.jsp

Render Command Requires below configurations:

  • Portlet Render URL with   mvcRenderCommandName as init parameter
    •  <portlet:renderURL var="viewLeave" > 
       <portlet:param name="backURL" value="<%=currentURL %>" /> 
       <portlet:param name="leaveId" value="${leave.leaveId}" /> 
       <portlet:param name="mvcRenderCommandName" value="viewleave_info"/>
       </portlet:renderURL>
  • OSGi component class need to annotate with @Component with below config:
    • @Component(
       property = {
       "javax.portlet.name=org_javasavvy_web_leave_portlet",
       "mvc.command.name=viewleave_info"
       },
       service = MVCRenderCommand.class
       )
    • Class need to implement com.liferay.portal.kernel.portlet.bridges.mvc.MVCRenderCommand
  • javax.portlet.name: The portlet name  is mandatory and make sure that Portlet component has javax.portlet.name
  • mvc.command.name:   This command name is used as value for mvcRenderCommandName init parameter in portlet render url

MVCActionCommand:

  • MVC Action Command is an interface that used to process particular action request. Suppose in Leave application, we need to process actions such as  “editLeave”, “approveLeave”  and we can create corresponding Action command classes.
  • Action Command eequires below configurations:
    • Portlet Action URL with
      • mvcActionCommand as init parameter
      •  "mvc.command.name" value in portlet action url name
      • <liferay-portlet:actionURL name="leave_editLeave" var="editLeave">
        </liferay-portlet:actionURL>
    • OSGi component class need to annotate with @Component with below config:
      • @Component(
         property = {
         "javax.portlet.name=org_javasavvy_web_leave_portlet",
         "mvc.command.name=leave_editLeave"
         },
         service = MVCActionCommand.class
         )
      • Class need to extends BaseMVCActionCommand or  implement MVCActionCommand
    • javax.portlet.name: The portlet name  is mandatory and make sure that Portlet component has javax.portlet.name
    • mvc.command.name:   This command name is used as value for mvcActionCommand  init parameter or in name in the action URL

 MVCResourceCommand:

  • MVC Resource Command is an interface that used to process particular resource request.   As you all know, MVCPortlet has only serveResource method and uses cmd or resourceId as custom command to process all resource requests, but Liferay 7 provide MVCResourceCommand to split up all resource requests.
    • Portlet Resource URL with  id with “mvc.command.name” value
      • <portlet:resourceURL id="delete_leave_res" var="deleteLeaveRes">
              <portlet:param name="leaveId" value="<%=String.valueOf(leaveId) %>"/>
        </portlet:resourceURL>
    • OSGi component class need to annotate with @Component with below config:
      • @Component(
         property = {
         "javax.portlet.name=org_javasavvy_web_leave_portlet",
         "mvc.command.name=delete_leave_res"
         },
         service = MVCResourceCommand.class
         )
      • Class need to extends BaseMVCResourceCommand or  implement MVCResourceCommand
    • javax.portlet.name: The portlet name  is mandatory and make sure that Portlet component has javax.portlet.name
    • mvc.command.name:   This command name value is used in “id” of portlet resource url

 

Click here to access all Portlet MVC  Tutorials along with code:

Liferay 7 MVC Render Command Example

Liferay 7 MVC Render Command Example will helps in understanding mvc render command along with code snippet. Let jump into the creation of MVC render command. Liferay 6 versions uses below approaches to redirect…

READ MORE

Liferay 7 MVCActionCommand Example

Liferay 7 MVCActionCommand Example helps developers in understanding creation of mvc action commands in liferay.  Let’s create for a sample action command class for editLeave in leave application. Please go through the below tutorial for understanding…
READ MORE

Liferay 7 MVC Resource Command Example

Liferay 7 MVC Resource Command Example tutorial helps in understanding Liferay 7 resource command. Let’s look into creation of sample resource command class for deleteLeave in leave application. Please go through the below tutorial for…
READ MORE

Hope this helps

5 thoughts on “Liferay 7 Portlet Commands Example”
  1. Admiring the time and effort you put into your site and detailed information you provide. It’s great to come across a blog every once in a while that isn’t the same unwanted rehashed material. Great read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

Comments are closed.