instanceof check in EL expression language

2019-01-11 06:21发布

Is there a way to perform an instanceof check in EL?

E.g.

<h:link rendered="#{model instanceof ClassA}">      
    #{errorMessage1}
</h:link>
<h:link rendered="#{model instanceof ClassB}">      
    #{errorMessage2}
</h:link>

7条回答
戒情不戒烟
2楼-- · 2019-01-11 06:54

Not very elegant as it mixes JSP EL and the earlier expression syntax, but doesn't require any extra Java code:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:set var="interfaceClass" value="<%=com.example.ClassA.class%>"/>
<c:set var="implementationClass" value="${model['class']}"/>
<c:if test="${interfaceClass.isAssignableFrom(implementationClass)}">
    <%-- Your logic here. --%>
</c:if>
查看更多
登录 后发表回答