【问题标题】:How to redirect to a page after printing with primefaces Printer?使用 primefaces 打印机打印后如何重定向到页面?
【发布时间】:2014-05-21 15:54:42
【问题描述】:

我使用了 primefaces 打印机,想在打印后重定向到上一页。我使用的打印机是这样的:

   <p:commandButton  value="Print" type="button" title="Print" actionListener="#{currentpage.redirect}"> 
        <f:ajax execute="@this"/>
        <p:printer target="printer" />
    </p:commandButton> 

在 currentpage bean 的重定向方法中,我删除了可以正常工作的记录,但如果我尝试将其重定向到上一页,它不会做任何事情。

public void redirect(ActionEvent actionevent) {
              /* Deleted the record */ 
}

如果我可以这样做或其他任何方式,请指导我。 提前致谢。

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    您的代码中有几个误解:

    • actionListener 方法不能触发重定向。这可以在action 中完成
    • ajax 请求无法触发重定向。 Ajax 旨在作为对服务器的异步请求,并为当前视图获取所需的结果,并处理更新视图的响应,而无需刷新页面或导航。
    • 如果使用 Primefaces 组件,您应该使用它们来提高页面效率。例如,&lt;p:commandButton&gt; 应该与&lt;p:ajax&gt; 一起使用,而不是&lt;f:ajax&gt;。但在这种情况下,&lt;p:commandButton&gt; 已经内置了 ajax 功能,因此无需使用任何这些 ajax 组件。

    知道了这个,你就知道你的设计应该改成这样了:

    <p:commandButton value="Print" type="button" title="Print"
        action="#{currentpage.redirect}" process="@this">
        <p:printer target="printer" />
    </p:commandButton>
    

    以及方法声明到:

    //parameterless
    public void redirect() {
        /* Deleted the record */ 
    }
    

    PrimeFaces 让您通过使用 oncomplete 属性在 ajax 请求完成时添加行为。此属性接收 javascript 函数的名称,该函数将在 ajax 请求完成且没有问题时立即调用。在这个方法中,你可以为你的重定向添加逻辑:

    <p:commandButton value="Print" type="button" title="Print"
        action="#{currentpage.redirect}" process="@this" oncomplete="redirect()">
        <p:printer target="printer" />
    </p:commandButton>
    
    <script type="text/javascript>
        redirect = function() {
            window.location.href = '<desired url>';
        }
    </script>
    

    【讨论】:

    • 试过了,但是 action 和 oncomplete 现在都不起作用了。
    • @niks 请不要说它不起作用,解释一下你有什么问题。
    • 我遇到的问题是它没有进入 currentpage bean 中的重定向功能,也没有进入 javascript 功能。它仅在单击按钮时打印。换句话说,没有记录获取删除以及它不会重定向到某些页面,它只有打印机正在单击按钮。
    • 这是我上面提到的问题。有意义吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    相关资源
    最近更新 更多