0

在过去的几天里,我一直被一个无法解决的问题所困扰。我正在Mojarra 2.2.12使用Wildfly 10.

我的问题是,当我在f:ajax标签内使用h:inputFile标签并尝试使用组件的 ID、、、或其他任何内容重新渲染任何组件时,不会重新渲染@form任何@all内容。曾经。但是,它在一个h:commandButton或其他组件中工作得非常好。

这是一个简单的例子。

我有一个xhtml名为index.xhtml. 此文件包含一个h:messageh:inputFileh:commandButton

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
    <h:form id="test" enctype="multipart/form-data">
        <h:message for="test" />
        <br />
        <h:inputFile value="#{uploadController.uploadedFile}">
            <f:ajax render=":test" />
        </h:inputFile>
        <h:commandButton value="Push me!"
            action="#{uploadController.buttonPushed}">
            <f:ajax render=":test" />
        </h:commandButton>
    </h:form>
</h:body>
</html>

我还有一个ManagedBean名为 的类UploadController,它在发生操作时记录消息,并且应该在h:message重新呈现表单后在标签内显示一条消息。

@ManagedBean
@ViewScoped
public class UploadController {

    private Part uploadedFile;

    public Part getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(Part uploadedFile) {
        this.uploadedFile = uploadedFile;
        System.out.println("File Uploaded!");
        FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("File Uploaded!"));
    }

    public void buttonPushed()
    {
        System.out.println("Button pushed!");
        FacesContext.getCurrentInstance().addMessage("test", new FacesMessage("Button Pushed!"));
    }
}

预期的结果是,当您选择一个文件时,它会“上传”该文件,然后显示“文件上传!” 一旦h:messageajax重新呈现页面。然后,如果您按下按钮,它将显示“按钮按下!” 在那一样h:message

实际结果是,是的,“文件已上传”。打印到控制台,但页面永远不会重新呈现,并且消息永远不会出现。另一方面,该按钮可以完美地工作,并且消息看起来就像它应该的那样。

我完全不知道从这里做什么。我是 JSF 的初学者。为什么 ajax 不会在 内部重新渲染h:inputFile,但在h:commandButton? 这是一个错误吗?如何在不刷新页面的情况下达到预期的效果?

4

0 回答 0