Service builder is out of box tool provided by liferay for code generation of service and persistence layer for custom define model .service builder generates all the boiler plate code for DAO and DTO layer for the application.
Liferay MVC portlet is light weight implementation of GenericPortlet class of JSR-168 standards and JSR-286 standards.
In MVC, there are three layers, and you can probably guess what they are.
- Model: The model layer holds the application data and logic for manipulating it.
- View: The view layer contains logic for displaying data.
- Controller: The middle man in the MVC pattern, the Controller contains logic for passing the data back and forth between the view and the model layers.
It also provides MVCActionCommand, MVCRenderCommand, MVCResourceCommand base classes to segregate logics of doProcess,doView,serveResource in their individual classes.These improves the modularity and code re-usability.
Detailed Description
In this sample, An example is illustrated to create a simple CRUD operation using Liferay MVC Porlet and Service Builder . So this would be the ideal example to perform insert,update,delete and read operation using portlet.
Service Builder in Liferay DXP & MVC Portlet for WEB (CRUD App).
1) Liferay Service Builder with OSGi module.
Create One Service Builder module project , Inside this service builder module project you have found two another module .
- Service Builder-Api
- Service Builder-Service
After specifying entity in service.xml you have to build your service builder module using gradle tool for building services classes and api.
2). MVC Portlet for WEB (CRUD App).
Create one another MVC Portlet module project for crud related operation.
In this module project, first you have to add compile time dependency in your portlet using build path .
So you can you your service builder’s api inside you web mvc portlet in build path.
For compile time dependency of service builder api module, you have to specify in your build.gradle file of web mvc portlet module project.
By adding one line in build.gradle file ..
compile project(":modules:IndividualRegistration:IndividualRegistration-api")
Then , bind your api LocalService interface in your portlet. By using @Reference annotation in your portlet.
Portlet Class: IndividualwebmvcportletPortlet.java
private IndividualLocalService individualLocalService; @Reference public void setIndividualLocalService(IndividualLocalService individualLocalService) { this.individualLocalService = individualLocalService; } @Override public void render(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException { System.out.println("Hey you are in render Method of IndividualregistrationwebmvcportletPortlet ...!!"); List<Individual> individuals=individualLocalService.getIndividuals(-1, -1); renderRequest.setAttribute("individuals", individuals); super.render(renderRequest, renderResponse); }
Click on Add New Individual Button user can add individual for this we have to create one
mvc render command class to handle this request. You have to specify portlet name
and command for which portlet and which command to be execute.
<portlet:renderURL var="addIndividualURL"> <portlet:param name="mvcRenderCommandName" value="addIndividual" /> <portlet:param name="redirect" value="<%= redirect %>" /> </portlet:renderURL>
Then you need to,Create one Render Command Class which implements MVCRenderCommand interface.
@Component( immediate = true, property = { "javax.portlet.name=individualweb_portlet", "mvc.command.name=addIndividual" }, service = MVCRenderCommand.class ) public class IndividualwebmvcAddRenderCommand implements MVCRenderCommand { @Override public String render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException { System.out.println("Hey you are in render Method of IndividualwebmvcAddRenderCommand...!!"); return "/addIndividual.jsp"; } }
When user click’s on Save Button at that time action command is used.
Add JSP page: addIndividual.jsp
<portlet:actionURL name="saveIndividual" var="saveIndividual" windowState="normal" /> <aui:form action="<%= saveIndividual %>" method="POST" name="fm"> <aui:fieldset> <aui:input type="hidden" name="redirect" value="<%= redirect %>" /> <aui:input type="hidden" name="individualId" value='<%= individual == null ? "" : individual.getIndividualId() %>'/> <aui:input name="firstName" label="individual-firstName" > <aui:validator name="required" errorMessage="individual-firstName-required"/> </aui:input> <aui:input name="lastName" label="individual-lastName" > <aui:validator name="required" errorMessage="individual-lastName-required"/> </aui:input> <aui:input name="skill" label="individual-skill" > <aui:validator name="required" errorMessage="individual-skill-required"/> </aui:input> <aui:input name="contactNo" label="individual-contactNo"> <aui:validator name="required" errorMessage="individual-contactNo-required"/> </aui:input> </aui:fieldset> <aui:button-row> <aui:button type="submit" /> <aui:button type="cancel" onClick="<%= viewIndividualURL %>" /> </aui:button-row> </aui:form>
@Component( immediate = true, property = { "javax.portlet.name=individualweb_portlet", "mvc.command.name=saveIndividual" }, service = MVCActionCommand.class ) public class IndividualwebmvcSaveActionCommand implements MVCActionCommand { private IndividualLocalService individualLocalService; private CounterLocalService counterLocalService; @Reference public void setCounterLocalService(CounterLocalService counterLocalService) { this.counterLocalService = counterLocalService; } @Reference public void setIndividualLocalService(IndividualLocalService individualLocalService) { this.individualLocalService = individualLocalService; } @Override public boolean processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException { System.out.println("Hey you are in processAction Method of IndividualwebmvcSaveActionCommand ...!!"); Individual individual = null; try { individual = individualFromRequest(actionRequest); } catch (PortalException e) { e.printStackTrace(); } ArrayList<String> errors = new ArrayList<String>(); if (IndividualValidator.validateIndividual(individual, errors)) { IndividualLocalServiceUtil.updateIndividual(individual); SessionMessages.add(actionRequest, "individual-saved"); } else { for (String error : errors) { SessionErrors.add(actionRequest, error); } PortalUtil.copyRequestParameters(actionRequest,actionResponse); actionResponse.setRenderParameter("mvcPath", "/addIndividual.jsp"); } return false; } private Individual individualFromRequest(PortletRequest request) throws PortalException { ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); Individual individual = null; Long id = ParamUtil.getLong(request, "individualId"); if (id > 0) { individual = individualLocalService.getIndividual(id); } else { individual = individualLocalService.createIndividual(counterLocalService.increment(Individual.class.getName())); } individual.setFirstName(ParamUtil.getString(request, "firstName")); individual.setLastName(ParamUtil.getString(request, "lastName")); individual.setSkill(ParamUtil.getString(request, "skill")); individual.setContactNo(ParamUtil.getString(request, "contactNo")); individual.setCompanyId(themeDisplay.getCompanyId()); individual.setGroupId(themeDisplay.getScopeGroupId()); return individual; }
For Edit Create another EditActionCommand class which implements MVCActionCommand. Create portlet action in your jsp for edit and passing id as parameter. Inside process Action method of MVCActionCommand class ,set one render parameter for rendering addIndividual.jsp page.
<portlet:actionURL name="editIndividual" var="editURL">
<portlet:param name="individualId" value="<%= String.valueOf(individualId) %>" />
</portlet:actionURL>
@Component( immediate = true, property = { "javax.portlet.name=individualweb_portlet", "mvc.command.name=editIndividual" }, service = MVCActionCommand.class ) public class IndividualwebmvcEditActionCommand implements MVCActionCommand { private IndividualLocalService individualLocalService; public IndividualLocalService getIndividualLocalService() { return individualLocalService; } @Reference public void setIndividualLocalService(IndividualLocalService individualLocalService) { this.individualLocalService = individualLocalService; } @Override public boolean processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException { System.out.println("Hey you are in processAction Method of IndividualwebmvcEditActionCommand...!!"); long individualId = ParamUtil.getLong(actionRequest, "individualId"); Individual individual=null; if (Validator.isNotNull(individualId)) { try { individual = individualLocalService.getIndividual(individualId); } catch (PortalException e) { e.printStackTrace(); } SessionMessages.add(actionRequest, "individual-deleted"); actionRequest.setAttribute("individual",individual); actionResponse.setRenderParameter("mvcPath","/addIndividual.jsp"); } else { SessionErrors.add(actionRequest, "null-individual-error"); } actionResponse.setRenderParameter("mvcPath","/addIndividual.jsp"); return false; }For Delete , Create another DeleteActionCommand class which implements MVCActionCommand. Create portlet action in your jsp for delete.
<portlet:actionURL name="deleteIndividual" var="deleteURL"> <portlet:param name="individualId" value="<%= String.valueOf(individualId) %>" /> <portlet:param name="redirect" value="<%= redirect %>"/> </portlet:actionURL>
@Component( immediate = true, property = { "javax.portlet.name=individualweb_portlet", "mvc.command.name=deleteIndividual" }, service = MVCActionCommand.class ) public class IndividualwebmvcDeleteActionCommand implements MVCActionCommand { private IndividualLocalService individualLocalService; @Reference public void setIndividualLocalService(IndividualLocalService individualLocalService) { this.individualLocalService = individualLocalService; } @Override public boolean processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException { System.out.println("Hey you are in deleteIndividual Method of IndividualwebmvcDeleteActionCommand ...!!"); long individualId = ParamUtil.getLong(actionRequest, "individualId"); if (Validator.isNotNull(individualId)) { try { individualLocalService.deleteIndividual(individualId); } catch (PortalException e) { e.printStackTrace(); } SessionMessages.add(actionRequest, "individual-deleted"); } else { SessionErrors.add(actionRequest, "null-individual-error"); } return false; } }
Inside @Component annotation , set "javax.portlet.name=individualweb_portlet",
Property.and also provide "mvc.command.name=deleteIndividual" for which action you have set this action command to be execute.
1 comments :
Very informative blog
Emoticon