【问题标题】:IF-ELSE condition in JSF form. Need to know right approachJSF 形式的 IF-ELSE 条件。需要知道正确的方法
【发布时间】:2012-11-27 19:29:42
【问题描述】:

我的表单上有 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 还是其他?

【问题讨论】:

标签: jsf-2


【解决方案1】:

确实只使用rendered 属性。如有必要,您可以将其包装在另一个根本不发出任何 HTML 的组件中,例如 &lt;h:panelGroup&gt;&lt;ui:fragment&gt;,这样您就不需要在所有后续组件上重复相同的 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>

请注意,&lt;h:outputLabel&gt; 生成的 HTML &lt;label&gt; 元素在语义上与您最初拥有的 &lt;s:text&gt; 具有完全不同的含义。您可能想改用 &lt;h:outputText&gt; 或者干脆忽略它。 JSF2/Facelets 只支持纯文本甚至模板文本中的 EL,不需要&lt;h:outputText&gt;

另见:

【讨论】:

  • 我理解你的意思,但我如何使用 将值保存在变量中并使用它在多个地方或者说需要在xhtml页面中使用变量才能使用?
  • 这是一个完全独立的问题,不过,请使用&lt;c:set&gt;
  • 我在这里提出了单独的问题stackoverflow.com/questions/13618397/…
【解决方案2】:

如果您想避免在“else”块中编写容易出错的条件否定,您可以利用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>

资源:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多