6

There is if-else condition on my form where I show heading and button text for add and update.

Below code what I have used in struts2 project and same code want to use in JSF2 project in xhtml page.

Struts2 Page

 <s:if test="person==null || person.id==null || person.id==''">
                <s:set var="buttonText" value="getText('person.button.add')"/>
                <s:text name="person.h1.add.text"/>
                <i><s:text name="person.smallfont.add.text"/></i>
            </s:if>
            <s:else>
                <s:set var="buttonText" value="getText('person.button.edit')"/>
                <s:text name="person.h1.edit.text"/>
                <s:text name="person.smallfont.edit.text"/>
            </s:else>

I could use JSTL in xhtml page and use above code as it is but I saw different approaches for this like below using EL. I am not sure but don't like below approach

<h:outputLabel value="Add Information" rendered="#{!empty personBean.person.id}" />
<h:outputLabel value="Use the form below to add your information." rendered="#{!empty personBean.person.id}" />

<h:outputLabel value="Update Information" rendered="#{empty personBean.person.id}" />
<h:outputLabel value="Use the form below to edit your information." rendered="#{empty personBean.person.id}" />

My Question:

Someone guide me how to use above code in IF-ELSE condition in JSF project please. Use EL/JSTL or any other?

Pirzada
  • 4,685
  • 18
  • 60
  • 113
  • 2
    Use the `rendered` attribute just like in your second example... take a look at this http://stackoverflow.com/a/4870557/617373 – Daniel Nov 27 '12 at 19:38

2 Answers2

15

Indeed just use the rendered attribute. You can if necessary wrap it in another component which doesn't emit any HTML at all, such as <h:panelGroup> or <ui:fragment>, so that you don't need to repeat the same rendered condition over all subsequent components.

<h:panelGroup rendered="#{not empty personBean.person.id}">
    Add Information
    <i>Use the form below to add your information.</i>
</h:panelGroup>
<h:panelGroup rendered="#{empty personBean.person.id}">
    Update Information
    <i>Use the form below to edit your information.</i>
</h:panelGroup>

Please note that <h:outputLabel> produces a HTML <label> element which has semantically a completely different meaning than the <s:text> which you initially have. You perhaps want to use <h:outputText> instead or just omit it altogether. JSF2/Facelets just supports plain text and even EL in template text without the need for <h:outputText>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I understand your point but how do I use to save value in variable and use it in multiple places or say need to use variable in xhtml page to be used? – Pirzada Nov 28 '12 at 04:22
  • This is a completely separate question, but ala, just use ``. – BalusC Nov 28 '12 at 11:30
  • I have asked separate question here http://stackoverflow.com/questions/13618397/declare-and-use-variable-in-jsf2-xhtml-page – Pirzada Nov 29 '12 at 03:34
2

If you want to avoid writing error-prone negation of condition in "else" block, you can utilize Composite components feature of JSF2 to create if-then-else component by yourself:

Definition:
Create file webapp/resources/my/if.xhtml with content:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
    <cc:attribute name="condition" required="true"/>
    <cc:facet name="then"/>
    <cc:facet name="else" />
</cc:interface>

<cc:implementation>
    <ui:fragment rendered="#{cc.attrs.condition}" >
        <cc:renderFacet name="then" />
    </ui:fragment>
    <ui:fragment rendered="#{not cc.attrs.condition}" >
        <cc:renderFacet name="else" />
    </ui:fragment>
</cc:implementation>
</html>

Usage:
Then you can use the component anywhere in your app. Tag name - if - binds to name of file containing component definition, the last part of tag xml namespace - my - is determined by name of directory containing component definition:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:my="http://java.sun.com/jsf/composite/my">

<f:view>
    <my:if condition="#{bean.property != null}">
        <f:facet name="then">
            <h:outputText value="#{bean.property}"/>
        </f:facet>
        <f:facet name="else">
            <h:outputText value="[null]"/>
        </f:facet>
    </my:if>
</f:view>
</html>

Resources:

czerny
  • 15,090
  • 14
  • 68
  • 96