Announcement Announcement Module
No announcement yet.
Checking for a specific error code Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking for a specific error code

    I want my JSP to set a boolean variable if a certain error code is included in the list of errors. The error code I want to check for is defined as a constant string named DELETED_RECORD in my class Message. The following is the code I've come with so far to set the variable (named RecordDeleted):

    <%@ page import = "my.package.Message" %>
    <%@ taglib prefix="c" uri="" %>
    <%@ taglib prefix="spring" uri="" %>
    <c:set var="recordDeleted" value="false" scope="page"/>
    <spring:hasBindErrors name="command">
    <c:forEach var="error" items="${errors.allErrors}">
    <c:if test="${error.code == '<%=Message.DELETED_RECORD%>'}">
    <c:set var="recordDeleted" value="true" scope="page"/>

    ... however it doesn't work. The problem is that it doesn't seem to translate the nested JSP expression <%=Message.DELETED_RECORD%> ... it just treats it as a literal. How do I get around this? Is there a better way to solve this problem?

    Thanks very much ...


  • #2
    <c:if test="${error.code == '<%=Message.DELETED_RECORD%>'}">
    I don't know for sure, but I'm guessing if that doesn't work it's becuase the <% %> tags don't work within an EL expression (${}).

    To get round this, the best way is to add Message.DELETED_RECORD to the model in your controller.
    Otherwise you could add it as a request attribute, e.g.
    <% request.setAttribute&#40;"delrecmessage", Message.DELETED_RECORD&#41;; %>
    and then refer to the attribute in your expression:
    <c&#58;if test="$&#123;error.code == delrecmessage&#125;">