If an object property is declared as of type Boolean (not primitive boolean) then there seem to be a problem in EL recognizing it!
Say you have the following object
class Case{
private Boolean valid;
public Boolean isValid(){
return this.valid;
}
public void setValid(Boolean val){
this.valid = val;
}
}
Say we put an object of type Case in the request under the name "case", then I try this in the JSP:
<td>Object is ${case.valid ? "Valid":"Invalid"} </td>
This gives me error "valid" is not a property of object Case! If I change valid from Boolean to primitive boolean it works!
Is this a known problem with Boolean types in EL that they are not recognized as boolean but as Java "normal" objects? What is the proper way to handle this?
Thanks
Typically, you use == and != with primitives such as int and boolean, not with objects like String and Color. With objects, it is most common to use the equals() method to test if two objects represent the same value.
When the Properties of your file are loaded you can use the Boolean -Class to get the Properties: Boolean. getBoolean("your. property");
The println(boolean) method of PrintStream Class in Java is used to print the specified boolean value on the stream and then break the line. This boolean value is taken as a parameter.
A boolean attribute should answer a simple yes/no question. Asking "is?" is a simple way to ask a yes/no question. So a boolean attribute name should complete the sentence is <attribute_name>
All the examples I've ever seen talk about boolean
properties that allow getters of the form isProperty()
in addition to getProperty()
and never Boolean
s.
I can't find any 'official' reference to this behaviour but this blog post seems to describe what I suspected when I commented initially - a Boolean
is an object while a boolean
is a primitive and while Java has auto-boxing, EL will ignore the isProperty()
getter that returns a Boolean
and will instead, look for a getProperty()
method.
So I suspect that, in your example, if you changed the return type of isValid()
to boolean
instead of Boolean
(but leave the type of the field as Boolean
), your EL expression will work as you expect.
EL treats Boolean as an object (which is totally correct) so it looks for getValid()
method. This is consistent with JavaBeans specification.
Try changing your property from Boolean
reference type to boolean
primitive type. If this is not possible and you're using new EL (i.e. 2.2 - I'm not sure about 2.1) you can invoke a method, so ${case.isValid()}
would be an example of a correct usage of this new EL feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With