【问题标题】:Method Not Found ? This not made sense [duplicate]找不到方法?这没有意义[重复]
【发布时间】:2017-10-08 19:33:00
【问题描述】:

我正在尝试在一个简单的 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>

我的 ManagedBean:

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()

【问题讨论】:

  • 你的方法有一个参数,但你没有在你的 EL 中传递一个参数。

标签: jsf file-upload primefaces methodnotfound


【解决方案1】:

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

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

请注意方法名后面不能有()

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 2017-05-22
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多