Liferay DXP MvcPortlet and Service Builder

Posted on by Alexandros Kazazis
Boy Ohh Boy Let's Do This! We will make a MvcPortlet with a Service builder which on a button click will store a message in the database and then display it. Starting with a clear Developer Studio Installation *Version 3.0.0.201606202116-ga1* ![ClearDevStudio][ClearDevStudio] Make sure that you have installed a Jdk ![JdkInstall][JdkInstall] Create a new Liferay 7 server *Liferay portal bundle:liferay-dxp-digital-enterprise-7.0-ga1* ![NewServer][NewServer] Then add a new Liferay Workspace Project ![WorkSpaceProject][WorkSpaceProject] Create a *servicebuilder* Liferay Module Project inside the Workspace Project ![ServiceBuilder][ServiceBuilder] and a *mvcportlet* Liferay Module Project ![MvcPortlet][MvcPortlet] Configure the `service.xml` and run the **buildService** Grandle Task. When the buildService task is done, refresh the project and the new classes will be appeared. This is how my `service.xml` looks like Alex TEST If you want to add some methods for CRUD in service Builder, run the **buildService** again. I added the following method in the `TextLocalServiceImpl.java` public Text addText(String message) { long numberid = counterLocalService .increment(); Text entry = textPersistence .create(numberid); entry .setMessage(message); entry .setNumberId(numberid); return textPersistence .update(entry); } Modify the `build.grandle` in the **MvcPortlet** project adding the dependencies with the **Service Builder** ``` compileOnly project("modules:service-builder:service-builder-api") ``` Continue with the `view.jsp` and `MvcportletmvcportletPortlet.java` My `view.jsp` looks like this: ```
<?php@ include file="/init.jsp" ?>

We wait for this: ${ message } ${ message1.message }
Name: ${ name }
Number: ${ number } ${ number1.numberId }

``` and the `MvcportletmvcportletPortlet.java`: package com.example.portlet; import com.example.model.Text; import com.example.service.TextLocalServiceUtil; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.model.User; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet; import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.util.WebKeys; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.Portlet; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.osgi.service.component.annotations.Component; @Component(immediate = true, property = { "com.liferay.portlet.display-category=category.sample", "com.liferay.portlet.instanceable=true", "javax.portlet.display-name=MvcPortlet Portlet", "javax.portlet.init-param.template-path=/", "javax.portlet.init-param.view-template=/view.jsp", "javax.portlet.resource-bundle=content.Language", "javax.portlet.security-role-ref=power-user,user" }, service = Portlet.class) public class MvcportletmvcportletPortlet extends MVCPortlet { private static final Log _log = LogFactoryUtil .getLog(MvcportletmvcportletPortlet.class); @Override public void render( RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest .getAttribute(WebKeys.THEME_DISPLAY); User user = themeDisplay .getUser(); if (user .getFirstName() == "") { renderRequest .setAttribute("name", "Stranger"); } else { renderRequest .setAttribute("name", user .getFirstName()); } if (renderRequest .getAttribute("message1") == null) { renderRequest .setAttribute("message", "we wait"); renderRequest .setAttribute("number", "we wait for this too"); } else { renderRequest .setAttribute("message1", renderRequest .getAttribute("result")); renderRequest .setAttribute("number1", renderRequest .getAttribute("result")); } super.render(renderRequest, renderResponse); } public void addEntry( ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { try { Text result = TextLocalServiceUtil .addText("this is what we waited for"); _log .info(result); actionRequest .setAttribute("result", result); } catch (Exception e) { _log .error(e); } } } Maybe my approach it's not the best, but it works fine in the case. Now continue with the deployment of the **MvcPortlet** , **Service Builder-api** and **Service Builder-service**. At this point all things should be good to go. Check if the deployed modules are active with go go shell. Run the following commands in command line: ``` telnet localhost 11311 ->enter lb ->enter ``` ![Telnet][Telnet] If the deployed modules are labeled as active, the application is ready to run. The portlet should look like this: ![Running][Running] and after the click of the button should look like this: ![Running2][Running2] [ClearDevStudio]: /sites/default/files/01.cleardevstudio.png [JdkInstall]: /sites/default/files/02.jdkinstall.png [NewServer]: /sites/default/files/03.newserver.png [WorkSpaceProject]: /sites/default/files/04.workspaceproject.png [ServiceBuilder]: /sites/default/files/05.servicebuilder.png [MvcPortlet]: /sites/default/files/06.mvcportlet.png [Telnet]: /sites/default/files/07.telnet.png [Running]: /sites/default/files/08.running.png [Running2]: /sites/default/files/09.running.png
Liferay DXP MvcPortlet and Service Builder
This entry was posted in Liferay and tagged liferay 7 by Alexandros Kazazis

If the deployed modules are labeled as active, the application is ready to run.