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