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?
source to share
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
orB
isnull
returnfalse
for==
oreq
,true
for!=
orne
.- If
A
orB
equalBigDecimal
, force andA
, andB
-BigDecimal
, and then:
- If the operator is
==
oreq
, returnA.equals(B)
- If the operator is
!=
orne
, return!A.equals(B)
- If
A
eitherB
equalsFloat
orDouble
enforces bothA
andB
beforeDouble
, apply operator- If
A
orB
equalBigInteger
, force andA
, andB
-BigInteger
, and then:
- If the operator is
==
oreq
, returnA.equals(B)
- If the operator is
!=
orne
, return!A.equals(B)
- If you
A
orB
thereByte
,Short
,Character
,Integer
orLong
are forced to likeA
andB
toLong
apply the operator- If
A
orB
equalsBoolean
force bothA
andB
beforeBoolean
, apply operator- If
A
orB
isenum
, enforce bothA
, andB
beforeenum
, use the operator- If
A
orB
equalString
, then forcing bothA
, andB
toString
, 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
orB
equalsBoolean
force bothA
andB
beforeBoolean
, apply operator
will give the priority of the method isXxx()
.
source to share