set boolean value into variable using JSTL tags?

前端 未结 5 2075
失恋的感觉
失恋的感觉 2021-02-06 22:30

I am using JSTL tags. i have below code.


Now variable refreshSent has boole

相关标签:
5条回答
  • 2021-02-06 22:30

    Yeah, it's can be boolean and String.

    <c:if test="${refreshSent}">
        your code...
    </c:if>
    

    or you can use like this

    <c:if test="${refreshSent eq 'false'}">
        your code...
    </c:if>
    

    Thanks.

    0 讨论(0)
  • 2021-02-06 22:31

    I use this for boolean

    <c:set var="refreshSent" value="${false}"/>
    <c:if test="${refreshSent}">
        some code ........
    </c:if>
    
    0 讨论(0)
  • 2021-02-06 22:46

    It is going to be a boolean. You can check it by comparing in a

    <c:if test="${refreshSent eq false}">
    

    and

    <c:if test="${refreshSent eq 'false'}">
    

    The second is a string comparison.

    0 讨论(0)
  • 2021-02-06 22:48
    <c:set var="refreshSent" value="false"/>
    

    If the expression in value evaluates to String; then the value of var refreshSent is of type String.

    See http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/ for reference.

    There is automatic type conversion done behind the scenes.

    See http://today.java.net/pub/a/today/2003/10/07/jstl1.html

    0 讨论(0)
  • 2021-02-06 22:54

    It is a String.

    The following JSP code:

    <c:set var="refreshSent" value="false" />
    <c:set var="refreshSent2" value="${false}" />
    <c:set var="refreshSent3" value="${'false'}" />
    
    <div>
    <div>${refreshSent} : ${refreshSent['class']}</div>
    <div>${refreshSent2} : ${refreshSent2['class']}</div>
    <div>${refreshSent3} : ${refreshSent3['class']}</div>
    </div>
    

    Outputs the following in a browser:

    false : class java.lang.String
    false : class java.lang.Boolean
    false : class java.lang.String
    

    But if you use the refreshSent variable in an EL expression where a boolean is expected, it will be converted to a boolean by a call to Boolean.valueOf(String) (according to the JSP Specification).

    So if you use:

    <c:if test="${refreshSent}">
    

    the value of the test attribute will be set to a boolean false value. The ${refreshSent} expression results in a String, but since the test attribute expects a boolean, a call to Boolean.valueOf("false") is made.

    0 讨论(0)
提交回复
热议问题