JSTL EL accessor translation priority when get and is both exist?

前端 未结 2 402
醉话见心
醉话见心 2021-01-24 07:32

This is sorta\' a two-part question.

I have a person object with a char attribute on it called \"active\". Person has a getActive() method that returns a char as expecte

相关标签:
2条回答
  • 2021-01-24 07:44

    Is my understanding correct regarding the char comparison? If so, is there any way to convert the types in JSTL so that they are comparable?

    From chapter 1.8.2 of EL 2.2 specification (emphasis mine):

    1.8.2       A {==,!=,eq,ne} B

    • If A==B, apply operator
    • If A is null or B is null return false for == or eq, true for != or ne.
    • If A or B is BigDecimal, coerce both A and B to BigDecimal and then:
      • If operator is == or eq, return A.equals(B)
      • If operator is != or ne, return !A.equals(B)
    • If A or B is Float or Double coerce both A and B to Double, apply operator
    • If A or B is BigInteger, coerce both A and B to BigInteger and then:
      • If operator is == or eq, return A.equals(B)
      • If operator is != or ne, return !A.equals(B)
    • If A or B is Byte, Short, Character, Integer, or Long coerce both A and B to Long, apply operator
    • If A or B is Boolean coerce both A and B to Boolean, apply operator
    • If A or B is an enum, coerce both A and B to enum, apply operator
    • If A or B is String coerce both A and B to String, compare lexically
    • Otherwise if an error occurs while calling A.equals(B), error
    • Otherwise, apply operator to result of A.equals(B)

    The char/Character is in EL thus coerced and evaluated as Long. That can never equal to a string literal of '1'.


    When both getActive() and isActive() exist, which one gets called by the EL translation? It seems isActive() gets priority but is there an officially documented ordering to this?

    From chapter 8.3.2 of Javabeans specification (emphasis mine):

    8.3.2 Boolean properties

    In addition, for boolean properties, we allow a getter method to match the pattern:

    public boolean is<PropertyName>();

    This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method.

    In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value.

    An example boolean property might be:

        public boolean isMarsupial();
        public void setMarsupial(boolean m);
    

    This, in combination with the point right after the emphasized point in the previously quoted chapter 1.8.2 of EL spec,

    • If A or B is Boolean coerce both A and B to Boolean, apply operator

    will give the isXxx() method precedence.

    0 讨论(0)
  • 2021-01-24 07:49

    What you can do is test the character's value, something like

    <c:if test="${person.active.value == 1}">Active</c:if>
    

    As to which one gets priority isXXX() or getXXX() it's pretty much implementation specific.

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