繁体   English   中英

有条件地从bean调用JavaScript

[英]Conditionally invoking JavaScript from a bean

如何在JSF 2中有条件地从bean调用JavaScript?

我有一个使用PrimeFaces的类,该类将用户文件上传到特定的文件夹,但是如果用户尝试上传与已经存在的文件名相同的文件,则我希望JavaScript在这种情况下在屏幕上打开一个框询问用户是否要覆盖旧文件,或取消操作。 如果不存在具有该名称的文件,则不会调用JavaScript。

我知道如何测试文件夹中是否包含特定文件,这只是我需要知道如何有条件地调用JavaScript。

我对此表示感谢。


我已经在线查看了variopus资源,但仍然无法使该应用程序正常工作。 基本上,这就是我所做的,在包含的xhtml页面中,我具有以下代码用于文件上传:

<p:fileUpload id="fileUpload" fileUploadListener="#{filters.upload}"
   allowTypes="#{filters.uploadTypes}" invalidFileMessage="#{filters.uploadBadType}"
   sizeLimit="#{filters.uploadSize}" invalidSizeMessag="#{filters.uploadBadSize}"
   update="fileUpload fileTable uploadMessage" description="Select Text File"
   disabled="#{filters.disableFileUploadButton}"/>

<!--- Then further in the same file is this: -->

<p:remoteCommand name="remoteCommandOverwrite" actionListender="#{filters.execOverwrite}"/>

包括上述内容的父xhtml页面我具有下面的JavaScript:

<script type="text/javascript">
  function popupConfirm() {
    var overwrite = confirm('Warning: This will overwrite the existing file - Do you confirm this?');
    if (overwrite) remoteCommandOverwrite([{name: overwrite, value: true}]);
  }
</script>

在我的bean中,我用以下三种方法编写了以下代码:

    public void upload(FileUploadEvent event) {
    FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);
    overwrite = false;
    // Do what you want with the file        
    try {
        copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public void copyFile(String fileName, InputStream in) {

     // Initialization etc.

                    File file = new File(uploadFull + fileName);
        if (file.exists()) {
            RequestContext.getCurrentInstance().execute("popupConfirm()");

// Then test to see if overwrite is true or false, and act accordingly

}

// Then I am supposed to get the value of overwrite here:
public void execOverwrite() {
    System.out.println("### execOverwrite() ###");
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    String soverwrite = (String) map.get("overwrite");
    if (soverwrite.equals("true")) {
        overwrite = true;
        System.out.println("overwrite: true");
    }
}

我想做的是首先有条件地调用JavaScript函数popupConfirm()。 在单击“上传”按钮时,如果条件为true,则调用此按钮,这就是我想要的。 然后应该打电话给

可以正常工作并调出Confirm()框,但是永远不会调用,因此也永远不会调用我的bean中的execOverwrite()方法,而且我无法拾取返回值并将其传递给copyFile()方法中的代码。 )。 怎么了?


我把这个问题放在后面的火炉上了大约一个星期,然后才重新解决。 我使它起作用,并且可以将值传递回Bean,但是以某种方式我需要从调用JavaScript的地方恢复执行。

概括地说,我的JavaScript包含以下代码:

<script type="text/javascript">
  function popupConfirm() {
    var overwrite = confirm('Warning: This will overwrite the existing file - Do you confirm this?');
    if (overwrite) remoteCommandOverwrite([{name: 'overwrite', value: 'true'}]);
  }
 </script>

在xhtml代码中,我有:

<p:fileUpload id="fileUpload" fileUploadListener="#{filters.upload}" ...../>

<!-- Other code -->

<p:remoteCommand name="remoteCommandOverwrite" actionListener="#{filters.execOverwrite}"/>

然后,在单击“选择文件”按钮之后单击“文件上载”按钮时,将执行上述JavaScript中的代码:

RequestContext.getCurrentInstance().execute("popupConfirm()");

然后,在对话框中单击“确定”,则在同一个bean中调用此方法:

public void execOverwrite() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    String soverwrite = map.get("overwrite");
    if (soverwrite.equals("true")) {
        overwrite = true;       }
    }
}

最终将测试“覆盖”标志,以查看其是否为真。

使用各种打印语句,我检查这是否有效。 但是,代码在遇到以下语句后不会继续执行:RequestContext.getCurrentInstance()。execute(“ popupConfirm()”); 无论我在对话框中输入“确定”还是“取消”,这都是我想要的。 看起来好像需要某种类型的回调,并且会最喜欢一些想法。

根据您的标签,您正在使用PrimeFaces,因此当浏览器完成处理服务器响应时,有一种简便的方法可以从服务器端事件托管bean方法调用javascript函数。 PrimeFaces为您提供了一个名为RequestContext的实用程序类。

public void doActionListenerMethod() {
  if (!isValid()) {
    RequestContext.getCurrentInstance().execute("MyJSObject.doClientSideStuff()");
  }
}

当JSF在客户端上完成渲染时,以下将把字符串参数作为Javascript执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM