【问题标题】:JSF/ Primefaces send two request to update input form and sent PDF back to browserJSF/ Primefaces 发送两个请求以更新输入表单并将 PDF 发送回浏览器
【发布时间】:2013-08-03 08:50:37
【问题描述】:

鉴于以下 jsf 页面

<h:form id="#{cc.clientId}>
    <p:inputText id="txt" value="#{controller.property}" required="true"/>
    <p:message for="txt"/>
    <p:commandButton actionListener="#{controller.print()}" update="@form" value="Print"/>
</h:form>

控制器

@Named
public Controller {
     private String property;

     public void setProperty() ...

     public String getProperty() ...

     public void print() {
           org.omnifaces.util.Faces.sendFile("".getBytes(), "file.pdf", true);
     }
}

成就是:

  1. 不输入 txt -> 显示有关所需值的错误消息
  2. 在 txt 中输入值 -> 将删除错误消息并将 PDF 发送回浏览器

会发生什么:

  1. 不输入 txt -> 显示有关所需值的错误消息
  2. 在 txt 中输入值 -> 错误消息未删除,PDF 被发送回浏览器

原因我很清楚,因为只向服务器发送了一个请求。

所以我尝试了:

<h:form id="#{cc.clientId}>
    <p:inputText id="txt" value="#{controller.property}" required="true"/>
    <p:message for="txt"/>
    <p:commandButton actionListener="#{controller.print()}" update="@form" value="Print">
         <p:ajax event="click" update="@form"/>
    </p:commandButton>
</h:form>

但仍然只有一个请求被发送到服务器。

旁注:在页面中显示 PDF 可以正常工作,但不再显示将表单放入 PDF 中。但首先我对发送两个请求的解决方案感兴趣。

提前谢谢,markus

【问题讨论】:

    标签: ajax jsf primefaces stream


    【解决方案1】:

    在 jsf 的验证阶段评估 required 属性,在服务器上处理。所以输入组件需要重新验证才能摆脱消息。

    我不太确定 Primefaces,但查看文档似乎“更新”本质上等同于 f:ajax 的“渲染”属性。

    如果我理解正确,使用“process”属性会将指定的组件提交给服务器进行处理,“update”将强制重新渲染。

    因此,将 ajax 标签更改为: &lt;p:ajax event="click" process="txt" update="txt"/&gt;

    此外,将事件更改为 keyup 事件将立即提供反馈并立即清除消息,而不是等待点击。

    编辑: 再澄清一点,需要将 ajax 调用移至输入标签并远离 commandButton。

    【讨论】:

    • 谢谢,实际上这更像是一种解决方法,因为最初我不想立即进行验证(真的想在打印前点击验证)。但实际上我会采用这种解决方法。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-10-18
    • 2019-01-02
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多