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 Liferay 7 MVC Commands:

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.READ MORE

Let’s jump into the tutorial:

  1. Add javax.portlet.name in LeavePortlet Controller  if not there
    1. @Component(
       immediate = true,
       property = {
       "com.liferay.portlet.display-category=category.sample",
       "com.liferay.portlet.instanceable=true",
       "javax.portlet.display-name=Leave Application",
       "javax.portlet.name=org_javasavvy_web_leave_portlet",
       "javax.portlet.init-param.template-path=/",
       "javax.portlet.init-param.view-action=/leave/view",
       "javax.portlet.init-param.view-template=/leave/view.jsp",
       "javax.portlet.resource-bundle=content.Language",
       "javax.portlet.security-role-ref=power-user,user"
       },
       service = Portlet.class
      )
      public class LeavePortlet extends MVCPortlet {
       
      }
  2. Create EditLeaveActionCommand  class and make that as OSGI Component with below config:
    1. @Component(
       property = {
       "javax.portlet.name=org_javasavvy_web_leave_portlet",
       "mvc.command.name=leave_editLeave"
       },
       service = MVCActionCommand.class
       )
      public class EditLeaveActionCommand extends BaseMVCActionCommand {
      
       private LeaveLocalService leaveService;
       
       @Reference(unbind = "-")
       protected void setLeaveService(LeaveLocalService leaveService) {
      
       this.leaveService = leaveService;
       }
       
       @Override
       protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) 
       throws Exception {
       
       SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
       String name = ParamUtil.getString(actionRequest, "name");
       System.out.println("Leave Edit"+name);
       Date startDate= ParamUtil.getDate(actionRequest, "startDate",sdf );
       Date endDate = ParamUtil.getDate(actionRequest, "endDate", sdf);
       
       ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
       long groupId = themeDisplay.getScopeGroupId();
       
       Leave leave = leaveService.addLeave(name, themeDisplay.getRealUserId(), groupId,
       themeDisplay.getCompanyId(), startDate, endDate);
       
       }
       
      }
  3.  In JSP,  portlet action URL should be like belows. We can use either of below action requests:
    1. <liferay-portlet:actionURL name="leave_editLeave" var="editLeave">
           <portlet:param name="mvcActionCommand" value="leave_editLeave" /> 
       </liferay-portlet:actionURL>
    2. <liferay-portlet:actionURL name="leave_editLeave" var="editLeave">
      </liferay-portlet:actionURL>
      <aui:form action="<%= editLeave %>" cssClass="container-fluid-1280" method="post" name="fm"> 
       <aui:form>

Access other MVC Command Examples:

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 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

Comments are closed.