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

时间:2012-11-27 19:29:43

标签: 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>

我可以在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}" />

我的问题:

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

2 个答案:

答案 0 :(得分:11)

确实只使用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>

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

另见:

答案 1 :(得分:2)

如果你想在&#34;否则&#34;中避免写错误的情况否定。阻止,您可以利用JSF2的复合组件功能自行创建if-then-else组件:

<强>定义:
使用内容

创建文件 webapp / resources / my / if.xhtml
<?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>

<强>用法:
然后,您可以在应用中的任何位置使用该组件。标记名称 - if - 绑定到包含组件定义的文件名,标记xml名称空间的最后一部分 - my - 由包含组件定义的目录名称确定:

<!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>

资源: