Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast with Expression Language

Tags:

java

casting

jsf

el

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

like image 433
Gondim Avatar asked Oct 18 '11 13:10

Gondim


People also ask

What is JSF expression language?

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.

What is expression language in JSP?

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.

What is unified expression language in Java?

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.

Which of the following will enable expression language in JSP?

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.


1 Answers

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>
like image 163
Bozho Avatar answered Oct 20 '22 00:10

Bozho