Liferay: Líder no Quadrante Mágico da Gartner pelo sexto ano consecutivo! Leia mais aqui

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 Anexos
62766 Visualizações
Média (5 Votos)
A média da avaliação é 4.6 estrelas de 5.
Comentários
Respostas do tópico Autor Data
nice clean and to the point post. One question... Dave Weitzel 21 de Maio de 2013 07:37
Another option for preventing default success... Matthias Thiel 2 de Julho de 2013 02:16
Nice article. One question through. I am using... Riley Pickerl 4 de Setembro de 2013 08:42
Nice post. Anil T 23 de Outubro de 2013 02:43
Take a look at this post:... pasquale rizzi 5 de Fevereiro de 2014 01: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?
Postado em 21/05/13 07: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.
Postado em 02/07/13 02: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
Postado em 04/09/13 08:42.
Postado em 23/10/13 02: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
Postado em 05/02/14 01:19.