-1

Using JSTL to compare strings keeps failing.

Environment TOMCAT 7.0.47 JRE6

I have already imported the taglib:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Here is the code:

    <c:forEach var="result" items="${resultList}">
        <tr>
            <td>${result.DIV}</td>
            <td>${result.DIV_NAME}</td>
            <td>${result.STAFF_CODE}</td>
            <td>${result.STAFF_NAME}</td>
             <td>
                <c:choose>
                    <c:when test="${result.STAFF_TYPE == 'DOC'}">DOCTOR</c:when>
                    <c:when test="${result.STAFF_TYPE == 'RES'}">RESIDENT</c:when>
                    <c:when test="${result.STAFF_TYPE == 'TRE'}">TRAINEE</c:when>
                    <c:otherwise>no</c:otherwise>
                </c:choose>
            </td>
            <td>${result.STAFF_FLAG == 1 ? 'A' : (result.STAFF_FLAG == 2 ? 'B' : '其他')}</td>
        </tr>
    </c:forEach>

The result.STAFF_TYPE part keeps failing and always goes into the <c:otherwise> block, displaying "no" on the page.

However, the result.STAFF_FLAG part works fine and shows 'A' or 'B' as expected.

I have already tried the following:

  • Changing == to eq:
<c:when test="${result.STAFF_TYPE eq 'DOC'}">DOCTOR</c:when>

But it didn't make any difference.

  • Printing out result.STAFF_TYPE to check if I really received the data:
<c:otherwise>${result.STAFF_TYPE}</c:otherwise>

This correctly showed 'DOC', 'RES', 'TRE', which means I am indeed receiving the data.

I have read the following articles:

Any help would be greatly appreciated.

Thanks.

jun
  • 1
  • 2
  • That can happen when the STAFF_TYPE values contain invisible whitespace characters. The comparison would then fail the same way in plain vanilla Java and this is then not a JSTL related problem. – BalusC May 26 '23 at 10:20
  • Thank you @BalusC ! After testing, it was indeed caused by invisible whitespace characters. I used **${fn:length(result.STAFF_TYPE)}** to print my data and found that the length was 4 instead of 3. I didn't expect it to be such a silly mistake, It even bothered me for two whole days lol – jun May 29 '23 at 02:39

1 Answers1

0

Thanks for @BalusC 's help.

After testing, it was indeed caused by invisible whitespace characters.

I used ${fn:length(result.STAFF_TYPE)} to check my data, and found out that the length is 4 instead of 3.

After adding fn:trim(), I solved my problem.

Here is the modified code:

<c:choose>
        <c:when test="${fn:trim(result.STAFF_TYPE) == 'DOC'}">DOCTOR</c:when>
        <c:when test="${fn:trim(result.STAFF_TYPE) == 'RES'}">RESIDENT</c:when>
        <c:when test="${fn:trim(result.STAFF_TYPE) == 'TRE'}">TRAINEE</c:when>
        <c:otherwise>${fn:trim(result.STAFF_TYPE)}</c:otherwise>
</c:choose>
jun
  • 1
  • 2
  • This is not a solution. This is a work around. You better take a step back and fix the data in the DB and then add converter/validator/constraint to ensure that this never happens anymore. – BalusC May 29 '23 at 10:39
  • @BalusC, I totally agree with you! It's time to talk to our DBA! – jun May 31 '23 at 13:25