Wednesday, 12 August 2020

Success & Error Message In Liferay DXP [Hide default error and success message in liferay dxp]

Today we will see how to add custom success and error messages along with how to hide default success and error messages in Liferay dxp

We will create an MVC portlet to demonstrate this tutorial. If you want to learn how to create an MVC portlet in Liferay DXP, refer to my previous tutorial of Create MVC Portlet 

Below would be our project structure.

 


What we will implement?

We will not connect with the database for this example, we have taken one list to demonstrate this example, once the list size exceeds 2 will give an error message & after adding the first 2 products we will give a custom success message, also we will hide both default success & error message.

How to hide default error messages in Liferay dxp?

We need to add the below code in ProcessAction to hide the default error message.

SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

How to hide default success messages in Liferay dxp?

We need to add the below property in @Component of our portlet class.

"javax.portlet.init-param.add-process-action-success-action=false"

Lets see example

Below is  ManageProductPortet,java where we have added code for success and error message based on business logic.

package com.demo.portlet;

import com.demo.constants.ManageProductPortletKeys;

import com.demo.model.Product;

import com.liferay.portal.kernel.log.Log;

import com.liferay.portal.kernel.log.LogFactoryUtil;

import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import com.liferay.portal.kernel.servlet.SessionErrors;

import com.liferay.portal.kernel.servlet.SessionMessages;

import com.liferay.portal.kernel.util.ParamUtil;

import com.liferay.portal.kernel.util.PortalUtil;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.portlet.ActionRequest;

import javax.portlet.ActionResponse;

import javax.portlet.Portlet;

import javax.portlet.PortletException;

import javax.portlet.ProcessAction;

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.header-portlet-css=/css/main.css",

"com.liferay.portlet.instanceable=true",

"javax.portlet.display-name=ManageProduct",

"javax.portlet.init-param.template-path=/",

"javax.portlet.init-param.view-template=/view.jsp",

"javax.portlet.name=" + ManageProductPortletKeys.MANAGEPRODUCT,

"javax.portlet.resource-bundle=content.Language",

"javax.portlet.security-role-ref=power-user,user",

"javax.portlet.init-param.add-process-action-success-action=false"

},

service = Portlet.class

)

public class ManageProductPortlet extends MVCPortlet {

private static final Log log=LogFactoryUtil.getLog(ManageProductPortlet.class);

private static List<Product> products=new ArrayList<Product>();

public void addProduct(Product product) {

products.add(product);

}

@Override

public void render(RenderRequest renderRequest, RenderResponse renderResponse)

throws IOException, PortletException {

super.render(renderRequest, renderResponse);

}

@ProcessAction(name="addProduct")

public void addProduct(ActionRequest actionRequest,ActionResponse actionResponse) {

String productName=ParamUtil.getString(actionRequest,"productName");

long productPrice=ParamUtil.getLong(actionRequest,"productPrice");

if(products.size()<2) {

Product product=new Product(productPrice, productName);

products.add(product);

SessionMessages.add(actionRequest, "success-key");

}else {

SessionErrors.add(actionRequest, "error-key");

SessionMessages.add(actionRequest, PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

}

}

Note -We will add custom message on view.jsp using success-key and error-key we have added above.

view.jsp

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

<portlet:renderURL var="addProductURL">

<portlet:param name="mvcPath" value="/addProduct.jsp"/>

</portlet:renderURL>

<aui:button onClick="${addProductURL}" value="add-product"></aui:button>

<liferay-ui:error key="error-key" message="You exceed max product limit." />

<liferay-ui:success key="success-key" message="Product saved successfully!"

/>

addProduct.jsp

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

<portlet:actionURL name="addProduct" var="addProductURL">

</portlet:actionURL>


<aui:form action="${addProductURL}">

<aui:input name="productName" label="productName"></aui:input>

<aui:input name="productPrice" label="productPrice"></aui:input>

<aui:input name="" type="submit" value="add-product"></aui:input>

</aui:form>

Below will be our expected output for error and success messages.


Backlink To Festival Images

No comments:

Post a Comment