【问题标题】:Programmatically control which components should be ajax-updated以编程方式控制应更新 ajax 的组件
【发布时间】:2013-09-09 12:13:56
【问题描述】:

我有一个复杂的表单,用户填写了几个字段,并且有两个选项:生成许可证文件或保存更改。如果用户单击生成许可证文件按钮而不保存更改,我会渲染一个带有错误消息的小组件,要求他在生成许可证之前保存。 为了显示带有警告消息的组件,我想使用 ajax 来避免渲染整个页面只是为了渲染警告组件。当然,如果更改已保存,则不需要警告消息,我将用户重定向到另一个页面。 我在可更改字段上有一个更改侦听器,以检测何时进行了更改。我不知道的是条件执行。 “如果未保存则使用 ajax 渲染或如果保存则重定向”部分。这是逻辑

if(saved){
  redirect();
}else{
  ajax.renderWarning()
}

--编辑-- 我将添加更多信息,因为我意识到我让事情变得过于开放。 这是可更新字段的一个示例。

<h:inputText name="computername3"  value="#{agreement.licenseServerBeans[2].computerId}" valueChangeListener="#{agreement.fieldChange}">
    <rich:placeholder value="Add Computer ID"/>
</h:inputText>

fieldChange() bean 方法

public void fieldChange(ValueChangeEvent event) {   
   change = true; //change is a boolean, obviously :P
}

这里是生成许可证按钮 jsf

<h:commandLink action="#{agreement.generateLicenseFile}">
    <span class="pnx-btn-txt">
        <h:outputText value="Generate License File" escape="false" />
    </span>
</h:commandLink>

这里是 generateLicenseFile() 方法

public String generateLicenseFile(){
    ....//lots of logic stuff
    return "/licenseGenerated.xhtml?faces-redirect=true";
}

【问题讨论】:

    标签: jsf jsf-2 ajax-update


    【解决方案1】:

    使用PartialViewContext#getRenderIds() 获取一个可变的客户端 ID 集合,这些客户端 ID 应该在当前的 ajax 请求中更新(它与您在 &lt;f:ajax render&gt; 中指定的完全相同,但随后以不带 @ 的绝对客户端 ID 的形式出现987654324@前缀):

    if (saved) {
        return "/licenseGenerated.xhtml?faces-redirect=true";
    }
    else {
        FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("formId:messageId");
        return null;
    }
    

    返回null 会导致它重新显示相同的视图。您甚至可以将其添加为全局 faces 消息,并让 ajax 命令在渲染中引用 &lt;h:messages&gt;

    if (saved) {
        return "/licenseGenerated.xhtml?faces-redirect=true";
    }
    else {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(...));
        return null;
    }
    

    <h:messages id="messages" globalOnly="true" />
    ...
    <f:ajax render="messages" />
    

    【讨论】:

    • 返回null不会刷新整个页面?
    • 如果您首先发送 ajax 请求,则不会。然后您就可以完全控制&lt;f:ajax render&gt; 属性。如果您没有首先发送 ajax 请求,那么它确实会“刷新”页面(不是因为它返回 null,而是因为它首先是一个同步(非 ajax)请求)。跨度>
    猜你喜欢
    • 2018-08-13
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2017-06-26
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多