0

我正在尝试在一个简单的 JSF/CDI 项目中实现 FileUpload 功能,具有正确的依赖项(我认为)和我在 primefaces 展示中找到的相同代码。

但是由于某种原因,FileUpload 事件在我的 ManagedBean 中没有找到监听器。

我希望我能在这里看到我缺少的东西,我已经检查了很多次代码,但我没有发现任何可能导致问题的东西。

我的 Xhtml 页面:

<html>
<h:head>
</h:head>

<body>
    <h:form>
        <p:fileUpload fileUploadListener="#{file.uploadHandler()}"
            mode="advanced" dragDropSupport="false" update="messages"
            sizeLimit="100000" fileLimit="3"
            allowTypes="/(\.|\/)(gif|jpe?g|txt)$/" />
        <p:growl id="messages" showDetail="true" />
    </h:form>
</body>
</html>

我的托管豆:

import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;

@Named
@ViewScoped
public class File implements Serializable {

    private static final long serialVersionUID = -6644472075906217176L;
    private String fileName;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void uploadHandler(FileUploadEvent event) {
        System.out.println("Nome do Arquivo: " + event.getFile().getFileName());
    }
}

我的 pom.file :

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.14</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>6.1</version>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

安慰:

15:59:36,935 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-27) /index.xhtml @16,45 fileUploadListener="#{file.uploadHandler()}": Method not found: class timesheet.business.bean.File.uploadHandler(): javax.el.MethodNotFoundException: /index.xhtml @16,45 fileUploadListener="#{file.uploadHandler()}": Method not found: class timesheet.business.bean.File.uploadHandler()
4

1 回答 1

0

fileUpload您应该在组件中指定方法引用

<p:fileUpload fileUploadListener="#{file.uploadHandler}"

请注意,方法名称后不应有() 。

于 2017-10-08T20:30:31.480 回答