1

How can I access SessionErrors object from JSP? I've tried

<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%>

<%= SessionErrors.contains(request, "error-invalid-value") %> <br>
<%= SessionErrors.size(request) %>

but the result is false and 0. However, the error tag works fine (it shows the error thrown).

<liferay-ui:error key="error-invalid-value" message="error-invalid-value" />

In my controller, I set up the error like this:

//request is an ActionRequest.
SessionErrors.add(request, "error-invalid-value");

I am trying to test whether "error-invalid-value" error has been thrown or not.

If it helps, here is the tag's source code (I couldn't understand anything from it): https://github.com/liferay/liferay-portal/blob/master/util-taglib/src/com/liferay/taglib/ui/ErrorTag.java

1 Answer 1

2

one way to use this is

<liferay-ui:error key="error-invalid-value" 
   message="translation-key-for-the-message-to-be-shown" />

In case you've called SessionErrors.add like you stated in the question, this will show the message referenced. Otherwise it will show nothing.

Another way to use it (as it's only static methods) it to just use its regular interface in scriptlets or in your own tags:

<% if(SessionErrors.size(renderRequest) > 2) { %>
   Wow, you have triggered a lot of errors
<% } %>

Check the full interface of SessionErrors to find more options to look for specific errors, access the whole keySet, or other information

Update: After your comments I've tried again and actually missed the PortletRequest parameters to SessionErrors: You can add errors with the HttpServletRequest or with the PortletRequest (e.g. actionRequest). When you add through the ActionRequest, you'll have to use the PortletRequest parameter to SessionErrors.doSomething, not the HttpServletRequest. On a JSP, request is a HttpServletRequest, while renderRequest is a PortletRequest. That's the one that you want to use in order to get your results.

3
  • Yes, that's how I use it for showing an error. However, I need to do other things with markup based on whether there was an error or not (e.g. open the modal). Is there a way to somehow use number of errors in an if condition?
    – Samuel
    Apr 24, 2015 at 9:36
  • 1
    Oops - sorry, now I'm re-reading the question. Will have to investigate. The interface mentions a HttpServletRequest, otherwise I'd have assumed a mismatch between PortletRequest (e.g. ActionRequest) and ServletRequest, but this doesn't seem to be the case - at least with regards to the interface. Stay tuned, can't do it immediately...
    – Olaf Kock
    Apr 24, 2015 at 16:07
  • 1
    I totally missed the PortletRequest version of the SessionErrors interface. See my update to the JSP snippet and the last paragraph explaining
    – Olaf Kock
    Apr 26, 2015 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.