JSTL EL access priority translate on get and both exist?

This is a two-part question.

I have a person object with a char attribute called "active". The person has a getActive () method that returns char as expected. In my JSTL EL, I have the following:

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

      

It never goes away. I understand that quoted literals in JSTL are strings (regardless of single or double quote) and that the char type is preserved from the getActive call, so the two values ​​are not equal when getActive () returns the character '1'.

Alternatively, I've added an isActive () method that returns a boolean value. In this case, the following work is performed:

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

      

Here are my questions:

  • How am I understanding char comparison? If so, is there a way to convert the types in JSTL so that they are comparable?
  • When are there both getActive () and isActive (), which is invoked by EL translation? It seems isActive () is getting priority, but is there an officially registered order for this?
+1


source to share


2 answers


How am I understanding char comparison? If so, is there a way to convert the types in JSTL so that they are comparable?

From section 1.8.2 EL 2.2 Specification (attention):

1,8,2   A {==,!=,eq,ne} B

  • If A==B

    , use the operator
  • If A

    null

    or B

    is null

    return false

    for ==

    or eq

    , true

    for !=

    or ne

    .
  • If A

    or B

    equal BigDecimal

    , force and A

    , and B

    - BigDecimal

    , and then:
    • If the operator is ==

      or eq

      , returnA.equals(B)

    • If the operator is !=

      or ne

      , return!A.equals(B)

  • If A

    either B

    equals Float

    or Double

    enforces both A

    and B

    before Double

    , apply operator
  • If A

    or B

    equal BigInteger

    , force and A

    , and B

    - BigInteger

    , and then:
    • If the operator is ==

      or eq

      , returnA.equals(B)

    • If the operator is !=

      or ne

      , return!A.equals(B)

  • If you A

    or B

    there Byte

    , Short

    , Character

    , Integer

    or Long

    are forced to like A

    and B

    to Long

    apply the operator
  • If A

    or B

    equals Boolean

    force both A

    and B

    before Boolean

    , apply operator
  • If A

    or B

    is enum

    , enforce both A

    , and B

    before enum

    , use the operator
  • If A

    or B

    equal String

    , then forcing both A

    , and B

    to String

    , compare lexically
  • Otherwise, if A.equals(B)

    an error occurs on the call , the error
  • Otherwise apply the operator to the result A.equals(B)

char

/ Character

is in EL, thus, it is forced and is evaluated as Long

. It can never be equal to a string literal '1'

.


When are there both getActive () and isActive (), which is invoked by EL translation? It seems isActive () is getting priority, but is there an officially registered order for this?



From chapter 8.3.2 of the Javabeans Specification (emphasis mine):

8.3.2 Boolean properties

Additionally, for boolean properties, we let the getter method match the pattern:

public boolean is<PropertyName>()

;

This "is <PropertyName>" method can be provided instead of the "get <PropertyName>" method, or it can be provided in addition to the "get <PropertyName>" method.

In any case, if the "is <PropertyName>" method is present for a boolean property, we will use the "is <PropertyName>" method to read the property value.

An example of a boolean property could be:

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

      

This, in conjunction with the dot immediately after the underlined dot in the previous chapter 1.8.2 EL spec,

  • If A

    or B

    equals Boolean

    force both A

    and B

    before Boolean

    , apply operator

will give the priority of the method isXxx()

.

+2


source


What you can do is check the value of a character, something like

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

      



As for the precedence of isXXX () or getXXX (), it is pretty much implementation specific.

0


source







All Articles