1

jQueryモバイルフレームワークでJSF2.1を使用するには、HTML5属性<h:commandLink/>をサポートするタグのカスタムレンダラーを作成する必要がありました。data-*

私のJSFマークアップとこのマークアップによって生成される出力は次のとおりです。

          <h:commandLink value="Prev" data-theme="e" data-role="button" data-inline="true" data-mini="true" data-icon="arrow-l"/>
           <h:commandLink value="Page 1 of 3" data-theme="e" data-role="button" data-inline="true" data-mini="true"/>
           <h:commandLink value="Next" data-theme="e" data-role="button" data-inline="true" data-mini="true" data-icon="arrow-r" data-iconpos="right"/>

ここに画像の説明を入力してください

私のカスタムレンダラーが2番目と3番目のタグを適切にレンダリングすることは明らかですが<h:commandLink/>、1番目のタグはレンダリングしません。最初のdata-*タグに属する属性は、直接の親<div/>タグでレンダリングされるようです。これは、Mojarra(私が使用している)の奇妙な(そしてバグのある)動作のようV 2.1.11です。これを克服する方法を教えてください。

私のカスタムレンダラーコードは次のとおりです。

public class MyCommandLinkRenderer extends CommandLinkRenderer {

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) {

        String[] attributes = {"data-theme", "data-role", "data-icon", "data-inline", "data-mini", "data-iconpos"};

        ResponseWriter writer = context.getResponseWriter();
        try {
            for (String attribute : attributes) {
                String value = (String) component.getAttributes().get(attribute);
                if (value != null) {
                    writer.writeAttribute(attribute, value, attribute);
                    System.out.println(attribute + " " + value);
                }
            }
            super.encodeBegin(context, component);
        } catch (Exception e) {
        }
    }
}
4

1 に答える 1

4

属性を書き込むsuper.encodeBegin() 前に呼び出す必要があります。

それ以外の場合は、生成されたHTML出力で確認されるように、前のHTML要素に属性を書き込んでいます。はsuper.encodeBegin()新しいHTML要素を開始します。

これは、Mojarraではなく、独自のコードの単なるバグです。


更新:また、最初からオーバーライドするのencodeBegin()は正しくありませんでした。LinkRenderer#writeCommonLinkAttributes()むしろメソッドをオーバーライドする必要があります。

@Override
protected void writeCommonLinkAttributes(ResponseWriter writer, UIComponent component) throws IOException {
    super.writeCommonLinkAttributes(writer, component);

    // ...
}

superちなみに、仕事の前後に電話してもかまいません。

于 2012-08-16T11:18:43.093 に答える