PDA

View Full Version : Checking for a specific error code


stevecnz
Jan 24th, 2005, 05:26 PM
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="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<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"/>
</c:if>
</c:forEach>
</spring:hasBindErrors>

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

Steve

cmgharris
Jan 25th, 2005, 04:22 AM
<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("delrecmessage", Message.DELETED_RECORD); %>

and then refer to the attribute in your expression:<c:if test="${error.code == delrecmessage}">

HTH