Interesting. I found this in an old project of mine.:
(this was a base controller, but could well be an utility method)
protected void addMessage(String key, boolean isError,
HttpServletRequest request, Object... args) {
List<Message> msgs = (List<Message>) request.getAttribute(MESSAGES_KEY);
if (msgs == null) {
msgs = new LinkedList<Message>();
}
Message msg = new Message();
msg.setMessage(msg(key, args));
msg.setError(isError);
msgs.add(msg);
request.setAttribute(MESSAGES_KEY, msgs);
}
and then in a messages.jsp
which was included in all pages I had:
<c:forEach items="${messages}" var="message">
//display messages here
</c:forEach>
MESSAGES_KEY
is a constant of mine with a value "messages" (so that it is later accessible in the forEach loop).
The Message
class is a simple POJO with those two properties. I used it for info messages as well as for custom non-validation errors.
This is a rather custom solution, but perhaps I hadn't found a built-in solution then. Google a bit more before using such a solution.