Using a JSP and MVC Portlet

An easy way to start developing a web application is to add markup to a JSP file and render it using a portlet Java class.

The W3E7 example application demonstrates this approach.

Here is the example portlet application.

The application has a JSP with markup content and an MVCPortlet class that renders the JSP. Deploy the example and examine it to learn how to create an application using a JSP with an MVC portlet.

Deploy a Simple MVC Portlet Module

Start a new Liferay instance by running

docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.112-ga112

Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.

Then, follow these steps to deploy the example:

  1. Download and unzip the example.

    curl https://resources.learn.liferay.com/dxp/latest/en/building-applications/developing-a-java-web-application/using-mvc/liferay-w3e7.zip -O
    
    unzip liferay-w3e7.zip
    
  2. Build and deploy the example.

    cd liferay-w3e7
    
     ./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
    
    note

    This command is the same as copying module JARs to /opt/liferay/osgi/modules on the Docker container.

  3. Confirm the deployment in the Docker container console.

    STARTED com.acme.w3e7.web_1.0.0
    
  4. Verify the application’s availability by adding the W3E7 Portlet widget from the Samples category to a widget page.

Here is the example portlet web application.

Congratulations, you’ve successfully built and deployed a new application.

Next, you’ll learn how to create this portlet application.

How to Create an Application Using MVC Portlet

There are two steps for building the example portlet:

  1. Create the view using a JSP.
  2. Create an MVCPortlet to register the application and render it.

Create a View Template

In the module’s src/main/resources/META-INF/resources folder, create a JSP file to be your view template. The file name is arbitrary; the example JSP file is view.jsp. Here is the JSP content.

<h4>W3E7 Portlet</h4>

<h5>Hello W3E7.</h5>

The markup above displays the heading “Hello W3E7.”

Create an MVCPortlet

In the module’s src/main/java folder, create a package called com.acme.w3e7.web.internal.portlet. In that package, add a class called W3E7Portlet that extends MVCPortlet.

public class W3E7Portlet extends MVCPortlet {
note

The *.web.internal.portlet part of the package name is a convention: web for the web module type, internal because a portlet implementation is private, and portlet because the class is a portlet.

This extension doesn’t require any additional methods; MVCPortlet’s built-in methods use component annotations (added next) to render the view.jsp template.

Configure the Portlet With Annotations

A @Component annotation configures the portlet.

@Component(
	property = {
		"com.liferay.portlet.display-category=category.sample",
		"javax.portlet.display-name=W3E7 Portlet",
		"javax.portlet.init-param.view-template=/view.jsp"
	},
	service = Portlet.class
)
public class W3E7Portlet extends MVCPortlet {
}

The service = Portlet.class attribute registers the class as a Portlet.

The property attribute’s value describes the portlet web application. The com.liferay.portlet.display-category=category.sample property adds the app to the sample widget category. The javax.portlet.display-name=W3E7 Portlet property specifies the app’s name.

The javax.portlet.init-param.view-template=/view.jsp property declares the view template path with respect to the application’s resources/META-INF/resources folder. When you add the portlet to a page, the resources/META-INF/resources/view.jsp view template renders.

note

The Portlet Descriptor to OSGi Service Property Map specifies how OSGi component property values map to traditional portlet descriptors.

What’s Next

Congratulations! You’ve created a web application using a JSP and one simple Java class. There are lots of directions you can go from here. To add more views, see Rendering Views with MVC Portlet. To add actions to your application, see MVC Action Command. Or implement a backend data model using Service Builder.

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy