Conditionally displaying JSF components

前端 未结 2 1979
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:02

First, I am new to Java EE, came from a strong ASP .NET development background. I have gone through the net, and I might miss this but it seems like there is no simple and s

相关标签:
2条回答
  • 2020-11-22 03:22

    In addition to previous post you can have

    <h:form rendered="#{!bean.boolvalue}" />
    <h:form rendered="#{bean.textvalue == 'value'}" />
    

    Jsf 2.0

    0 讨论(0)
  • 2020-11-22 03:23

    Yes, use the rendered attribute.

    <h:form rendered="#{some boolean condition}">
    

    You usually tie it to the model rather than letting the model grab the component and manipulate it.

    E.g.

    <h:form rendered="#{bean.booleanValue}" />
    <h:form rendered="#{bean.intValue gt 10}" />
    <h:form rendered="#{bean.objectValue eq null}" />
    <h:form rendered="#{bean.stringValue ne 'someValue'}" />
    <h:form rendered="#{not empty bean.collectionValue}" />
    <h:form rendered="#{not bean.booleanValue and bean.intValue ne 0}" />
    <h:form rendered="#{bean.enumValue eq 'ONE' or bean.enumValue eq 'TWO'}" />
    

    Note the importance of keyword based EL operators such as gt, ge, le and lt instead of >, >=, <= and < as angle brackets < and > are reserved characters in XML. See also this related Q&A: Error parsing XHTML: The content of elements must consist of well-formed character data or markup.

    As to your specific use case, let's assume that the link is passing a parameter like below:

    <a href="page.xhtml?form=1">link</a>
    

    You can then show the form as below:

    <h:form rendered="#{param.form eq '1'}">
    

    (the #{param} is an implicit EL object referring to a Map representing the request parameters)

    See also:

    • Java EE 6 tutorial - Expression Language
    • How to show JSF components if list is not null and has size() > 0
    • Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
    0 讨论(0)
提交回复
热议问题