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 archivos adjuntos
32878 Accesos
Promedio (3 Votos)
La valoración media es de 4.666666666666667 estrellas de 5.
Comentarios
Respuestas anidadas Autor Fecha
nice clean and to the point post. One question... Dave Weitzel 21 de mayo de 2013 7:37
Another option for preventing default success... Matthias Thiel 2 de julio de 2013 2:16
Nice article. One question through. I am using... Riley Pickerl 4 de septiembre de 2013 8:42
Nice post. Anil T 23 de octubre de 2013 2:43
Take a look at this post:... pasquale rizzi 5 de febrero de 2014 1:19

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?
Publicado el día 21/05/13 7:37.
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.
Publicado el día 2/07/13 2:16.
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
Publicado el día 4/09/13 8:42.
Publicado el día 23/10/13 2:43.
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
Publicado el día 5/02/14 1:19.