6

私のフォームには、追加と更新の見出しとボタン テキストを表示する 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 またはその他を使用しますか?

4

2 に答える 2

15

実際、rendered属性を使用するだけです。<h:panelGroup>必要に応じて、やなどの HTML をまったく出力しない別のコンポーネントにラップして、後続のすべてのコンポーネント<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 要素を生成することに注意してください。代わりに使用するか、完全に省略したい場合があります。JSF2/Facelets はプレーン テキストをサポートするだけでなく、テンプレート テキストの EL もサポートします。<label><s:text><h:outputText><h:outputText>

以下も参照してください。

于 2012-11-28T00:03:29.867 に答える