Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read Boolean property in JSP EL [duplicate]

Tags:

jsp

el

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

like image 755
DhafirNz Avatar asked Apr 18 '11 01:04

DhafirNz


People also ask

Can you use == for boolean in Java?

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.

How read boolean value from properties file in Java?

When the Properties of your file are loaded you can use the Boolean -Class to get the Properties: Boolean. getBoolean("your. property");

Can you print boolean in Java?

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.

How do you name boolean attributes?

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>


2 Answers

All the examples I've ever seen talk about boolean properties that allow getters of the form isProperty() in addition to getProperty() and never Booleans.

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.

like image 127
no.good.at.coding Avatar answered Oct 12 '22 12:10

no.good.at.coding


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.

like image 30
Pedro Avatar answered Oct 12 '22 12:10

Pedro