Search code examples
javaxhtmlrendered-attribute

Render link when not null


As I 'll show up a table on a page , there are certain elements that have no value . But the elements that have a value to form a link to another page . Right now it shows nothing , neither it has value or not.

I'm new to Java and do not understand what I have written wrong in my code .

Here is my code:

<h:link outcome="schoolclass-detail" value="#{la.participant.schoolclass.classname}" rendered="{!la.participant.schoolclass.classname==null}">
                        <f:param name="schoolclassId" value="#{la.participant.schoolclass.id}" />
                </h:link>

Thanks for all help in advance!


Solution

  • Assuming that you use JSF, 'la' is your backing bean, and you want to render your link if the variable of 'classname' is not null, I would suggest to use EL expressions rather than raw java expressions.

    You can read more about EL here

    The expression of your Example

    rendered="{!la.participant.schoolclass.classname==null}"

    could look in EL like

    rendered="#{not (la.participant.schoolclass.classname eq null)}"
    

    or

    rendered="#{la.participant.schoolclass.classname ne null}"
    

    or maybe better

    rendered="#{not (empty la.participant.schoolclass.classname)}"
    

    The not obviously inverts the following expression

    The eq stands for equals and therefor is equal to java object1.equals(object2);, or in case of primitive types like boolean obj1 == obj2;

    The ne stands for not equals. You might guess what it does...

    The empty evaluates the following expression to be in a defined state of empty covering stuff like null, empty String ("") or array or list with no contents

    Also important is the # in front of the statement, that you seem to have forgotten in your example. If you forget this, the value of your rendered attribute is treated as a String, and not some expression that should be evaluated by JSF. I think that is the main cause for your example not to work, since your expression is basically correct

    Best Regards

    J.Adam