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?
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>
.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With