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-succes