【问题标题】:Two consecutive actions for Command Button in JSFJSF 中命令按钮的两个连续动作
【发布时间】:2017-04-21 23:11:15
【问题描述】:

我正在尝试实现一个场景,其中会有一个上传按钮,当有人成功上传文件时,用户将被转发到主页。

我有两个按钮,一个用于导入,另一个用于关闭当前页面并返回主页,但我需要在上传按钮上也包含返回主页功能,并且返回应该在文件完成时发生成功上传。我还针对错误的扩展名和未选择文件实现了错误消息,因此只有在文件上传成功后才会返回。

请原谅我的不成熟。我是前端开发的新手。 请建议我可以与 .XHTML(JSF) 和 java Bean 集成的解决方案,因为这是我唯一的选择。 提前致谢。

【问题讨论】:

  • 嗯,我不确定你是如何实现的(可能是一些代码?),但一种方法是你可以在执行重定向的 p:fileUpload oncomplete 属性上附加一个 javascript 函数?跨度>
  • 嘿,你能帮我看看我们需要写的java脚本吗? @yassine
  • 你试过<p:fileUpload ... oncomplete="window.location='index.xhtml'" />吗?
  • 由于某种原因,我的 oncomplete/onsuccess 标签无法正常工作。我不明白为什么.. :/

标签: jsf jsf-2 commandbutton


【解决方案1】:

您可以通过这种方式获得oncomplete 属性:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>
</h:head>
<f:view>
    <h:form>
        <p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced"
                      oncomplete="window.location='index.xhtml'"
                      update="messages" auto="true" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
        <p:growl id="messages" showDetail="true" />
    </h:form>
</f:view>
</html>

使用简单的托管 bean:

@ManagedBean
public class FileUploadView {

    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

【讨论】:

  • 创意解决方案
【解决方案2】:

只有当一切顺利时,您才能以编程方式从您的 JSF 操作方法重定向。

redirect from jsf?

您可以在您的 handleFileUpload 方法中执行此操作。

【讨论】:

    猜你喜欢
    • 2012-08-17
    • 2017-03-27
    • 1970-01-01
    • 2011-07-09
    • 2013-10-24
    • 1970-01-01
    • 2013-06-07
    • 2017-10-04
    • 2013-04-30
    相关资源
    最近更新 更多