Search code examples
jspstruts2ognl

Conditional checks in OGNL


I want to represent the following JSTL code in Object Graph Navigation Language (OGNL).

<c:set var="idError"><s:fielderror fieldName="transporterId"/></c:set>
<c:set var="chkError"><s:fielderror fieldName="chk"/></c:set>
<c:set var="currentPageError"><s:fielderror fieldName="currentPage"/></c:set>

<c:if test="${not empty idError or not empty chkError or not empty currentPageError}">
    <div class="errorblock">
        <s:fielderror fieldName="transporterId"/>
        <s:fielderror fieldName="chk"/>
        <s:fielderror fieldName="currentPage"/>
    </div>
</c:if>

I have tried the following.

<s:set var="idError"><s:fielderror fieldName="transporterId"/></s:set>
<s:set var="chkError"><s:fielderror fieldName="chk"/></s:set>
<s:set var="currentPageError"><s:fielderror fieldName="currentPage"/></s:set>

<s:if test="%{(#idError!=null and #idError!='') or (#chkError!=null and #chkError!='') or (#currentPageError!=null and #currentPageError!='')}">
    <div class="errorblock">
        <s:fielderror fieldName="transporterId"/>
        <s:fielderror fieldName="chk"/>
        <s:fielderror fieldName="currentPage"/>
    </div>
</s:if>

This works only for the first conditional check, #idError!=null and #idError!=''. The rest of the conditions are never evaluated to true?

What is the correct way to represent the given JSTL code using OGNL?


Solution

  • I don't know about the problem in question but in this case, I was trying to display error messages based on whether a particular field has an error or not.

    In this case, since field errors are stored in the fieldErrors map, specific field errors can be checked using <s:if> like as follows.

    <s:if test="fieldErrors.containsKey('transporterId') or fieldErrors.containsKey('chk') or fieldErrors.containsKey('currentPage')">
        <div class="errorblock">
            <s:fielderror fieldName="transporterId"/>
            <s:fielderror fieldName="chk"/>
            <s:fielderror fieldName="currentPage"/>
        </div>
    </s:if>
    

    This is sufficient to suit my requirements.


    This is also possible by using the following simple conditional check as usual.

    <s:if test="hasFieldErrors()">
        <div class="errorblock">
            <s:fielderror fieldName="transporterId"/>
            <s:fielderror fieldName="chk"/>
            <s:fielderror fieldName="currentPage"/>
        </div>
    </s:if>
    

    But in this case, I need to check only for a selected fields to see if they have generated some errors and not for all the fields which are submitted by a request. Hence, there is a need to check for all those selected fields (not all) like the first case.

    These selected fields are generally hidden fields or query-string parameters.

    Any errors generated by them are displayed at one place in a block whereas any errors generated by the rest of the fields like input components such as <s:textfield> are displayed just beside them.