2

次のコードを検討してください。

<h:commandButton value="do" action="#{testBacking.do}">
   <f:ajax execute="@all" render="@all" listener="#{testBacking.listener}"/>
</h:commandButton>

次のようなカスタム タグ (サーバー ロジックに基づく値を持つ) を Ajax 応答 XML に含めたいと考えています。

<isValidationFailed> true </isValidationFailed>

検証が失敗した場合、このデータを使用してボタン (ダブルクリックを避けるために Ajax の開始時に無効にされていた) を再度有効にすることができます。

どうすればこれを達成できますか (できれば JSF サードパーティ ライブラリを使用せずに)。

編集:

サンプルコードは、より正確には次のようになります。

<h:commandButton id="myButton" value="do" action="#{testBacking.do}">
 <f:ajax execute="id1" render="id2 myButton" listener="#{testBacking.listener}"/>
</h:commandButton>
4

1 に答える 1

7

PartialViewContextこれは、 を使用して JSF アプリケーションにロードするカスタムでのみ可能ですPartialViewContextFactory。カスタムは、次にカスタムをPartialViewContext返す必要があります。このカスタムでは、とinを呼び出して、XML 応答に拡張機能を追加できるはずです。何かのようなもの:PartialResponseWriterPartialViewContext#getResponseWriter()PartialResponseWriterstartExtension()endExtension()endDocument()

@Override
public void endDocument() throws IOException {
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put("name1", "value1");
    attributes.put("name2", "value2");
    startExtension(attributes);
    write("lorem ipsum");
    endExtension();
    super.endDocument();
}

これは、XML 応答で次のようになります。

<extension name1="value1" name2="value2">lorem ipsum</extension>

data.responseXMLこれは関数内で利用可能であり、トラバース可能ですjsf.ajax.addOnEvent()


特定のケースでそれをどのように利用できるかの完全なキックオフの例を次に示します。

MyPartialViewContextFactoryカスタム部分ビュー コンテキストを提供します。

public class MyPartialViewContextFactory extends PartialViewContextFactory {

    private PartialViewContextFactory wrapped;

    public MyPartialViewContextFactory(PartialViewContextFactory wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public PartialViewContext getPartialViewContext(FacesContext context) {
        return new MyPartialViewContext(wrapped.getPartialViewContext(context));
    }

}

MyPartialViewContextカスタム部分応答ライターを提供します。

public class MyPartialViewContext extends PartialViewContextWrapper {

    private PartialViewContext wrapped;
    private PartialResponseWriter writer;

    public MyPartialViewContext(PartialViewContext wrapped) {
        this.wrapped = wrapped;
        this.writer = new MyPartialResponseWriter(wrapped.getPartialResponseWriter());
    }

    @Override
    public PartialResponseWriter getPartialResponseWriter() {
        return writer;
    }

    @Override
    public void setPartialRequest(boolean isPartialRequest) {
        wrapped.setPartialRequest(isPartialRequest);
    }

    @Override
    public PartialViewContext getWrapped() {
        return wrapped;
    }

}

MyPartialResponseWriter<extension id="myextension">本文を JSON として書き込みます):

public class MyPartialResponseWriter extends PartialResponseWriter {

    public MyPartialResponseWriter(ResponseWriter wrapped) {
        super(wrapped);
    }

    @Override
    public void endDocument() throws IOException {
        startExtension(Collections.singletonMap("id", "myextension"));
        write("{\"validationFailed\": " + FacesContext.getCurrentInstance().isValidationFailed() + "}"); // Consider a JSON serializer, like Google Gson.
        endExtension();
        super.endDocument();
    }

}

実行するには、次のようにファクトリを登録しますfaces-config.xml

<factory>
    <partial-view-context-factory>com.example.MyPartialViewContextFactory</partial-view-context-factory>
</factory>

にアクセスし、解析し、使用する方法は<extension id="myextension">次のjsf.ajax.addOnEvent()とおりです。

jsf.ajax.addOnEvent(function(data) {
    if (data.status == "success") {
        var args = JSON.parse(data.responseXML.getElementById("myextension").firstChild.nodeValue);

        if (args.validationFailed) {
            // ...
        }
        else {
            // ...
        }
    }
});

ただし特定の機能要件は、別の、おそらくより単純な方法で実現できます。ajax リクエストでボタン自体を更新し、ポストバックが成功する手段がある場合にボタンのdisabled属性を評価するだけです。true

<h:commandButton id="myButton" value="do" action="#{testBacking.do}" 
    disabled="#{facesContext.postback and not facesContext.validationFailed}">
    <f:ajax execute="id1" render="@this id2" listener="#{testBacking.listener}"/>
</h:commandButton>
于 2012-08-30T13:33:44.667 に答える