Announcement Announcement Module
Collapse
No announcement yet.
Showing global errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Showing global errors

    Hi,

    In my controller I create a global error mesage like follows:

    Code:
        errors.reject("badCredentials","Bad credentials");
    Of course, my .properties file contains and entry for the badCredentials key.

    In the JSP, I have code like the following at the top to output the global errors:

    Code:
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
    
    (irrelevant part of JSP not shown here)
    
            <spring:hasBindErrors name="command">
                <c:forEach var="err" items="${errors.globalErrors}">
                    <fmt:message key="${err.code}"/><br/>
                </c:forEach>
            </spring:hasBindErrors>
    
    (more JSP code)
    This code correctly looks up the error code "badCredentials" from the properties file. If the key isn't there however, the default that I specified in the controller code isn't shown - of course this is true as I'm only outputting err.code translated.

    The type of err is ObjectError which implements the MessageSourceResolvable interface. Is there a spring tag to consume an object of type err and produce the text I want (i.e. reverting to a default and inserting arguments in places where the messages have argument placeholders) or some nice way of doing this in a .jsp file?

    Thanks,

    PUK

  • #2
    ** solved **

    It was this easy:
    Code:
            <spring:hasBindErrors name="command">
                <c:forEach var="err" items="${errors.globalErrors}">
                    <spring:message message="${err}"/><br/>
                </c:forEach>
            </spring:hasBindErrors>
    I was surprised it was so trivial as I'd search the Internet for ages before posting and could only find complicated solutions (including writing a separate tab library!).

    Thanks,

    PUK

    Comment

    Working...
    X