Is it possible to cast using EL?
I've got a class Vehicle, and two other classes Car and Bus that extends Vehicle. I'm searching for all Vehicles and there's some data that has in Bus but does not have in Car.
So I was trying to show things from Car when it's a Car and things from Bus when it's a Bus.
How could I do it, Cast, instanceof? And How would I do it, cause i'm kinda lost here.
Thanks
JSF provides a rich expression language. We can write normal operations using #{operation-expression} notation. Following are some of the advantages of JSF Expression languages. Can reference bean properties where bean can be an object stored in request, session or application scope or is a managed bean.
JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical.
To summarize, the new, unified expression language allows page authors to use simple expressions to perform the following tasks: Dynamically read application data stored in JavaBeans components, various data structures, and implicit objects. Dynamically write data, such as user input into forms, to JavaBeans components.
To enable the EL expression in a JSP, developers need to use following page directive. To get a better idea, on how expression works in a JSP, we will see the below example where EL is used as an operator to add two numbers and get the output.
You can use ${obj.class.simpleName == 'Car'}
but it's not the best thing thing to do.
Perhaps you can have a geType()
abstract method and use it to differentiate. For example:
<c:forEach items="${vehicles}" var="vehicle">
Reg.No: ${vehicle.registrationPlateNumber}
<c:if test="${vehicle.type == 'bus'}">
Toilets: ${vehicle.toilets}
</c:if>
</c:forEach>
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