Liferay DXP MvcPortlet and Service Builder

Posted on 28/12/2016 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


Make sure that you have installed a Jdk


Create a new Liferay 7 server
Liferay portal bundle:liferay-dxp-digital-enterprise-7.0-ga1


Then add a new Liferay Workspace Project


Create a servicebuilder Liferay Module Project inside the Workspace Project


and a mvcportlet Liferay Module Project


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

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "">
<service-builder package-path="com.example">
    <!--<entity data-source="sampleDataSource" local-service="true" name="Foo" remote-service="false" session-factory="sampleSessionFactory" table="foo" tx-manager="sampleTransactionManager uuid="true"">-->
    <entity name="Text" local-service="true">
        <column name="numberId" type="long" primary="true"></column>
        <column name="message" type="String"></column>
        <finder name="number" return-type="collection">
            <finder-column name="numberId"></finder-column>

If you want to add some methods for CRUD in service Builder, run the buildService again.

I added the following method in the

    public Text addText(String message) {
        long numberid = counterLocalService
        Text entry = textPersistence
        return textPersistence

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

My view.jsp looks like this:

<%@ include file="/init.jsp" %>

<portlet:actionURL name="addEntry" var="addEntryURL"></portlet:actionURL>

<form action="${ addEntryURL }" method="post">
    <aui:button name="add"  value="add" type="submit"></aui:button>
    <B> We wait for this: ${ message } ${ message1.message }</B><BR>
    <B> Name: ${ name }</B><BR>
    <b> Number: ${ number } ${ number1.numberId }</b><BR>

and the

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 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 = {
    "javax.portlet.display-name=MvcPortlet Portlet",
}, service = Portlet.class)
public class MvcportletmvcportletPortlet extends MVCPortlet {
    private static final Log _log = LogFactoryUtil
    public void render(
        RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {
        ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest
        User user = themeDisplay
        if (user
            .getFirstName() == "") {
                .setAttribute("name", "Stranger");
        else {
                .setAttribute("name", user
        if (renderRequest
            .getAttribute("message1") == null) {
                .setAttribute("message", "we wait");
                .setAttribute("number", "we wait for this too");
        else {
                .setAttribute("message1", renderRequest
                .setAttribute("number1", renderRequest
        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");
                .setAttribute("result", result);
        catch (Exception 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


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

The portlet should look like this:


and after the click of the button should look like this:


Liferay DXP MvcPortlet and Service Builder
This entry was posted in Liferay and tagged liferay 7 by Alexandros Kazazis

Add new comment

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