关于jsf 2:采用JSF格式的IF-ELSE条件。需要知道正确的方法

IF-ELSE condition in JSF form. Need to know right approach

窗体上有if-else条件,其中显示用于添加和更新的标题和按钮文本。

下面的代码是我在struts2项目中使用的代码,而相同的代码想在xhtml的JSF2项目中使用页。

Struts2页面

1
2
3
4
5
6
7
8
9
10
 <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"/>
                <s:text name="person.smallfont.add.text"/>
            </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的方法。我不确定但不喜欢下面的方法

1
2
3
4
5
<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或其他?


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

1
2
3
4
5
6
7
8
<h:panelGroup rendered="#{not empty personBean.person.id}">
    Add Information
    Use the form below to add your information.
</h:panelGroup>
<h:panelGroup rendered="#{empty personBean.person.id}">
    Update Information
    Use the form below to edit your information.
</h:panelGroup>

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

另请参见:

  • 有条件地显示JSF组件


如果要避免在" else "块中编写容易出错的条件否定条件,则可以利用JSF2的复合组件功能自行创建if-then-else组件:

定义:使用内容创建文件webapp / resources / my / if.xhtml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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-由包含组件定义的目录的名称确定:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!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>

资源:

  • Mkyong的教程
  • DevManuals教程
  • 参考文档