1
votes

I need to use a template for various classes and I'm trying to avoid the evaluation of the code inside the c:if because the bean class has getPrintResource() only if is extending a specific class.

I'm trying

<c:if test="${bean instanceOf PrintableClass}">...

<c:if test="${! empty bean.printResource}">...

to avoid the evaluation of this element

<c:if test="${...}">
   <tag:printResource id="printBtn"
    rendered="#{bean.printable}"
    resource="#{bean.printResource}" 
       label="#{msg.print}">
   </tag:printResource>
</c:if>

The only result achieved is the Property "printResource" not found on *bean*


Edit

Even if I am able to build a valid test for the c:if, the attribute resource would be evaluated as Property "printResource" not foundanyway.
My question is about avoiding the evaluation of a block of code if a test is false


Maybe my design is wrong or this isn't possible...
Thank you

2
Regarding your edit: the block inside the if won't be evaluated if the test is false, There shouldn't be any problem. - JB Nizet

2 Answers

0
votes

Create a custom EL function that checks it. Something like

<c:if test="${myFn:isPrintResourcePossible(bean)}">

An EL function is just a static method in some class, declared in a tag library descriptor.

See http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html for a tutorial.

0
votes

You can check if the bean is an instance of the expected class:

<c:if test="${bean.getClass().name =='my.package.PrintableClass'}">

Or another equivalent condition:

<c:if test="${bean['class'].simpleName eq 'PrintableClass'}">