p:fileUpload里面的p:对话框丢失@ViewScoped值 [英] p:fileUpload inside p:dialog losing @ViewScoped values

查看:166
本文介绍了p:fileUpload里面的p:对话框丢失@ViewScoped值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用< p:fileUpload> 更新多个文件。在我上传文件后,我设置了一个列表,路径工作正常。

I'm trying to update multiple files with <p:fileUpload>. After I upload the files, I set a List with the paths that works fine.

之后,用户必须填写一些其他可选信息,然后点击一个按钮(提交表单)。

After that the user then has to fill some other optional information and then click on a button(to submit the form).

当用户点击按钮所有关于在丢失。

When the user clicks on the button all the information about the list that was done on the public void handleFileUpload(FileUploadEvent event) is lost.

只有当用户点击按钮时,我才需要在数据库上保存路径,我不了解为什么值丢失,我使用 @ javax.faces.view.ViewScoped

I need to save the paths on the database only when the user clicks on the button, i don't understand why the values are being lost, I'm using @javax.faces.view.ViewScoped

另外 handleFileUpload 正在处理用户在屏幕上输入的输入不可用。

Also when the handleFileUpload is being processed the inputs made on the screen by the user are not yet available.


  • JSF 2.2

  • CDI

  • PrimeFaces 5.1

我将省略以下代码中的一些部分,以避免使其巨大(如果您认为没有足够的信息告诉我)

I will omit some parts on the code below to avoid making it huge (if you think there's not enough info just tell me)

XHTML:

<h:form>
    <!-- OMITED -->
    <p:dialog>
        <!-- OMITED -->
        <p:fileUpload fileUploadListener="#{csrBean.handleFileUpload}"
            mode="advanced"
            skinSimple="true"
            cancelLabel="Cancelar"
            multiple="true"
            auto="false"/>
        <!-- OMITED -->
    </p:dialog>
    <!-- OMITED -->
</h:form>

方法:

public void handleFileUpload(FileUploadEvent event) {
    UploadedFile file = event.getFile();
    String normalize = FilenameUtils.normalize("uploads/csr/"
            + csr.getNumero() + "/" + event.getFile().getFileName());
    File destino = new File(normalize);
    try {
        FileUtils.copyInputStreamToFile(file.getInputstream(), destino);
    } catch (IOException e) {
        e.printStackTrace();
    }

    CsrOsAnexo anexo = new CsrOsAnexo();
    anexo.setCaminho(normalize);
    anexo.setOs(csr.getRespostaRecente().getOs());      
    csr.getRespostaRecente().getOs().getAnexoList().add(anexo);


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

调试,我可以看到 csr.getRespostaRecente ).getOs()。getAnexoList()填充所有档案路径,但一旦 handleFileUpload()结束,我转到该方法由commandButton调用,这些值已经消失,表单的值被填充。

Debugging, I can see the csr.getRespostaRecente().getOs().getAnexoList() filled with all the archives paths but as soon as the handleFileUpload() ends and I go to the method called by the commandButton, those values are gone and the values of the form are filled.

推荐答案

模态对话框必须有自己的表单。

A modal dialog must have its own form.

<h:body>
    ...
    <p:dialog>
        <h:form>
            ...
        </h:form>
    </p:dialog>
</h:body>

因为,当模态对话框生成到HTML输出中时,它是由JavaScript重定位到HTML的结尾< body> ,导致它不再以任何形式出现。这种重新定位是必要的,以保证与具有模式覆盖和z索引的故障的旧版浏览器(阅读:IE< 9)的兼容性。生成的HTML DOM树最终看起来像这样(使用webbrowser的开发工具查看它):

Because, when the modal dialog is generated into HTML output, it's by JavaScript relocated to the end of HTML <body> which causes it to not be sitting in any form anymore. This relocation is necessary to guarantee compatibility with older browsers (read: IE<9) having trouble with modal overlays and z-indexes. The generated HTML DOM tree ends up to look like this (use webbrowser's dev tools to see it):

<body>
    ...
    <form>
        ...
    </form>
    ...
    <div class="ui-dialog ...">
        ...
    </div>
</body>

文件上传显然仍然没有使用表单,因为它会自动创建一个隐藏的iframe,其中包含一个表单模拟ajax体验。但是,任何其他操作基本上都会丢失JSF视图状态,因此任何视图范围的bean都将被重新创建。

That the file upload apparently still worked without the form is because it autocreates a hidden iframe with therein a form to simulate the "ajax experience". However, any other action would basically lose the JSF view state and any view scoped bean would therefore get recreated.

这篇关于p:fileUpload里面的p:对话框丢失@ViewScoped值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆