JSF形式的IF-ELSE条件.需要知道正确的方法

Pir*_*ada 5 jsf-2

我的表单上有if-else条件,我在其中显示标题和按钮文本以进行添加和更新.

下面我在struts2项目中使用的代码和相同的代码想在xhtml页面的JSF2项目中使用.

Struts2

 <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>
Run Code Online (Sandbox Code Playgroud)

我可以在xhtml页面中使用JSTL并使用上面的代码,但我在下面使用EL看到了不同的方法.我不确定,但不喜​​欢下面的方法

<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}" />
Run Code Online (Sandbox Code Playgroud)

我的问题:

有人指导我如何在JSF项目的IF-ELSE条件中使用上面的代码.使用EL/JSTL还是其他?

Bal*_*usC 13

确实只是使用rendered属性.如果需要,您可以将其包装在另一个根本不会发出任何HTML的组件中,例如<h:panelGroup><ui:fragment>,这样您就不需要rendered在所有后续组件上重复相同的条件.

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,<h:outputLabel>生成一个HTML <label>元素,其语义与<s:text>您最初的含义完全不同.您可能想要使用<h:outputText>而不是完全省略它.JSF2/Facelets只需支持纯文本甚至模板文本中的EL而无需使用<h:outputText>.

也可以看看: