Create Spring Portlet in Liferay 7

 


In the first versions of Liferay DXP, it was difficult to create spring portlets but in the latest version of the Liferay IDE, it has been made so simple to create a Spring Portlet.

1. Create A Module Project:

- Open Liferay developer studio and create a new Liferay spring Mvc Portlet project

- Add a name for your project: themeray-spring-mvc-portlet


- Click Next button and add the package name: com.themeray.mvcportlet


- Click Finish, Then You can see the project directory structure as shown below.




2. Portlet Controller Class:



package com.themeray.mvcportlet.controller;

import com.themeray.mvcportlet.dto.User;

import com.liferay.portletmvc4spring.bind.annotation.ActionMapping;
import com.liferay.portletmvc4spring.bind.annotation.RenderMapping;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Locale;

import javax.portlet.ActionResponse;
import javax.portlet.MutableRenderParameters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;

/**
 * @author innotall
 */
@Controller
@RequestMapping("VIEW")
public class UserController {

	@ModelAttribute("user")
	public User getUserModelAttribute() {
		return new User();
	}

	@RenderMapping
	public String prepareView() {
		return "user";
	}

	@RenderMapping(params = "javax.portlet.action=success")
	public String showGreeting(ModelMap modelMap) {

		DateFormat dateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy G");

		Calendar todayCalendar = Calendar.getInstance();

		modelMap.put("todaysDate", dateFormat.format(todayCalendar.getTime()));

		return "greeting";
	}

	@ActionMapping
	public void submitApplicant(
		@ModelAttribute("user") User user, BindingResult bindingResult,
		ModelMap modelMap, Locale locale, ActionResponse actionResponse,
		SessionStatus sessionStatus) {

		_localValidatorFactoryBean.validate(user, bindingResult);

		if (!bindingResult.hasErrors()) {
			if (_logger.isDebugEnabled()) {
				_logger.debug("firstName=" + user.getFirstName());
				_logger.debug("lastName=" + user.getLastName());
			}

			MutableRenderParameters mutableRenderParameters =
				actionResponse.getRenderParameters();

			mutableRenderParameters.setValue("javax.portlet.action", "success");

			sessionStatus.setComplete();
		}
		else {
			bindingResult.addError(
				new ObjectError(
					"user",
					_messageSource.getMessage(
						"please-correct-the-following-errors", null, locale)));
		}
	}

	private static final Logger _logger = LoggerFactory.getLogger(
		UserController.class);

	@Autowired
	private LocalValidatorFactoryBean _localValidatorFactoryBean;

	@Autowired
	private MessageSource _messageSource;

}

Annotations In Spring MVC Portlet

@Controller 

When we use this annotation in the class The Spring Bean Container Considers the Class as a Portlet Controller which handles all the portlet requests comes to the portlet 

@RequestMapping 

As we know there are different modes of portlet such as VIEW, EDIT and HELP, We can configure the portlet to handle the request only for a specific mode, in our case we are going to use the Portlet Controller only for handling requests in VIEW mode, so it will be @RequestMapping("VIEW")

@ActionMapping 

In Liferay Portlet MVC we have processAction() method to handle the Action Requests comes to the portlet, similarly we can define Action Methods for Action URLs using this Annotation

@RenderMapping 

We can use the @RenderMapping annotation to handle the Render Requests for Render URLs, We can Return the Page Name in the Render Methods, the returned page will be rendered in the portlet.

@ResourceMapping 

In Liferay Portlet MVC we have serveResource() method to handle the Resource Requests, similarly we can define Resource Methods for Resource URLs using this annotation with an id

3. Web.xml File:

web.xml is the root file for spring portlet, Spring Portlet Configurations starts from this file.



<?xml version="1.0"?>

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-context/portlet-application-context.xml</param-value>
	</context-param>
	<servlet>
		<servlet-name>ViewRendererServlet</servlet-name>
		<servlet-class>com.liferay.portletmvc4spring.ViewRendererServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>ViewRendererServlet</servlet-name>
		<url-pattern>/WEB-INF/servlet/view</url-pattern>
	</servlet-mapping>
	<filter>
		<filter-name>delegatingFilterProxy</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>delegatingFilterProxy</filter-name>
		<url-pattern>/WEB-INF/servlet/view</url-pattern>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
	</filter-mapping>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

4. Portlet-Application-Context.Xml File:

View Resolver Configurations can be made in this file


<?xml version="1.0"?>

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>
	<context:annotation-config />
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
		<property name="contentType" value="text/html;charset=UTF-8" />
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jspx" />
		<property name="viewClass" value="com.liferay.portletmvc4spring.PortletJstlView" />
	</bean>
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>content.Language</value>
			</list>
		</property>
		<property name="defaultEncoding" value="UTF-8" />
	</bean>
	<bean id="springSecurityPortletConfigurer" class="com.liferay.portletmvc4spring.security.SpringSecurityPortletConfigurer" />
	<bean id="delegatingFilterProxy" class="org.springframework.web.filter.DelegatingFilterProxy">
		<property name="targetBeanName" value="springSecurityFilterChain" />
	</bean>
</beans>

  • - contentType : view character encoding
  • - prefix : view Files Path
  • - suffix : view (File)Type
  • - viewClass : View Resolver class for Markup language

5. build.gradle File:


buildscript {
	repositories {
		maven {
			url "https://repository-cdn.liferay.com/nexus/content/groups/public"
		}
	}

	dependencies {
		classpath group: "com.liferay", name: "com.liferay.gradle.plugins.css.builder", version: "3.0.3"
	}
}

apply plugin: "com.liferay.css.builder"

dependencies {
	compile group: "com.liferay.portletmvc4spring", name: "com.liferay.portletmvc4spring.framework"
	compile group: "com.liferay.portletmvc4spring", name: "com.liferay.portletmvc4spring.security"
	compile(group: "org.hibernate.validator", name: "hibernate-validator") {
		exclude group: "javax.validation", module: "validation-api"
	}
	compile group: "org.springframework", name: "spring-aop"
	compile group: "org.springframework", name: "spring-beans"
	compile group: "org.springframework", name: "spring-context"
	compile group: "org.springframework", name: "spring-core"
	compile group: "org.springframework", name: "spring-expression"
	compile group: "org.springframework", name: "spring-jcl"
	compile group: "org.springframework", name: "spring-web"
	compile group: "org.springframework", name: "spring-webmvc"
	compile group: "org.springframework.security", name: "spring-security-config"
	compile group: "org.springframework.security", name: "spring-security-core"
	compile group: "org.springframework.security", name: "spring-security-web"

	compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
	providedCompile group: 'javax.portlet', name: 'portlet-api', version: '3.0.0'
	compileOnly group: "javax.servlet", name: "javax.servlet-api"
	compileOnly group: "javax.validation", name: "validation-api"
	compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations"
	cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"

	portalCommonCSS group: "com.liferay", name: "com.liferay.frontend.css.common", version: "4.0.0"
	compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
	compile 'org.springframework.boot:spring-boot-starter-validation:2.3.0.RELEASE'
}

war {
	dependsOn buildCSS
	exclude "**/*.scss"

	filesMatching("**/.sass-cache/") {
		it.path = it.path.replace(".sass-cache/", "")
	}

	includeEmptyDirs = false
}

You need to add these dependencies to resolve some compilation issues

  • - compileOnly group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
  • - compile 'org.springframework.boot:spring-boot-starter-validation:2.3.0.RELEASE'

0 comments: