User Feedback - Success and Error Messages in Portlets

When users are interacting with our applications they usually expect some feedback. This feedback can be a success message if everything went well or an error message if anything went wrong.

Even though there are many frameworks for achieving this functionality, Liferay portlets have one mechanism that can be used for this purpose.

Success Messages #

Adding your own Success Message #

There are several options to display your own sucess messages after executing the actions phase of a portlet:

  • Passing the parameter "successMessage" to the actionRequest
  • In the process action of your portlet you can call this method:
    SessionMessages.add(actionRequest, "request_processed", "this-is-my-custom-success-message") 
    this-is-my-custom-success-message can be translated in Language.properties
  • If you want to display the successMessage somewhere else you can use the taglib
    <liferay-ui:success />

Hiding the default Success Message #

By default, Liferay always displays a default success message when an action was executed and no exceptions were thrown.

  • This message can be overwritten from a Language Hook using the key:
    your-request-completed-successfully 
  • If you want Liferay not do display the default success message after an specific action, you would just need to add this code in your process action method
    SessionMessages.add(actionRequest, (LiferayPortletConfig)portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
    This will only work in version 6.2 and above
  • If you want Liferay not to display the default success message at all in your portlet after any action, you would need to add this to your portlet.xml:
    <init-param><name>add-process-action-success-action</name><value>false</value></init-param>

Error Messages #

Adding your own Error Messages #

In the process Action of your portlet, you need to add this code:

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

and in the jsp of your portlet:

<liferay-ui:error key="error-key" message="this-is-error-message-1" /><liferay-ui:error key="error-key-2" message="this-is-error-message-2" />

Hiding the default Error Message #

By default, Liferay always displays a default error message when an exceptions was thrown in your process Action method.

  • This message can be overwritten from a Language Hook using the key:
    your-request-failed-to-complete 
  • If you want Liferay not do display the default error message after an specific action, you would just need to add this code in your process action method
    SessionMessages.add(actionRequest, (LiferayPortletConfig)portletConfig.getPortletName() + SessionMessages. KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
    This will only work in version 6.1 and above

More Advanced Customization #

For deeper customization, this logic can be overwritten in a jspf from a hook: html/common/themes/portlet_messages.jspf

Changing the look and feel of these messages can be achieved in the theme overwriting the classes:

.portlet-msg, .portlet-msg-error, .portlet-msg-success

0 Attachments
30173 Views
Average (3 Votes)
The average rating is 4.666666666666667 stars out of 5.
Comments
Threaded Replies Author Date
nice clean and to the point post. One question... Dave Weitzel May 21, 2013 7:37 AM
Another option for preventing default success... Matthias Thiel July 2, 2013 2:16 AM
Nice article. One question through. I am using... Riley Pickerl September 4, 2013 8:42 AM
Nice post. Anil T October 23, 2013 2:43 AM
Take a look at this post:... pasquale rizzi February 5, 2014 1:19 AM

nice clean and to the point post.
One question though? at what point are session errors cleared automatically?
I seem to have them retained across requests or at least:
SessionErrors.isEmpty(actionRequest) reports false even though no error is displayed to user apart from failed to complete.
This happens if I have an error (say telephone format) and user then corrects it and resubmits. So I need to clear them manually?
Posted on 5/21/13 7:37 AM.
Another option for preventing default success message is to call

SessionMessages.add()

during the portlet action with a key that is not defined on JSP. It seems that default message is only shown, if no other SessionMessage has been defined.
Since the custom message is not known in JSP, it will just display nothing.
Tested with Version 6.1.
Posted on 7/2/13 2:16 AM.
Nice article. One question through. I am using Vaadin and there is no JSP. How can I request for the UI to display the SessionMessages that I have set in java code and not from JSP. Is it possible?

Thanks
Posted on 9/4/13 8:42 AM.
Posted on 10/23/13 2:43 AM.
Take a look at this post: https://www.liferay.com/it/community/forums/-/message_boards/message/16710057

If I use portletConfig.getPortletId() instead of portletConfig.getPortletName() default error message hiding also works...so we hope you change the documentation.

Thanks
Posted on 2/5/14 1:19 AM.