Categories: java, spring, spring-el

How to properly check for Boolean value in Spring EL expression?

1 answer

I'm a bit new to using Spring Expression Language but I'm trying to see what is the best way to check a that a Boolean value is not null and true?

For type "Display" there is a property "removeMessage" which is of type java.lang.Boolean. So I want to first check that this value is not null and that it is true. Based on if this is true or not I will either return an empty string or a calculatedMessage.

I am currently checking with below expression:

display?.removeMessage != null && display?.removeMessage ? '' : display.calculatedMessage 

While this works, I was hoping to find a more nicer way to do the null and true check. I was thinking/hoping for something more like how we would check normally such as e.g. BoolenUtils.isTrue(value) (apache), or even just checking like Boolean.TRUE.equals(value) which takes care of the null check.

All answers to this question, which has the identifier 61216005

The best answer:

Just having this should work , and already include your null check:

display?.removeMessage ? null : display.calculatedMessage 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data