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 3rd 方库)?

编辑:

更准确地说,示例代码应该是这样的:

<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于您使用PartialViewContextFactory. 自定义PartialViewContext应该反过来返回一个自定义PartialResponseWriteron PartialViewContext#getResponseWriter()。在此自定义PartialResponseWriter中,您应该能够通过调用startExtension()endExtension()in向 XML 响应添加扩展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 回答