p:fileUpload prime faces

时间:2012-04-01 11:56:10

标签: java jquery ajax jsf jsf-2

听众没有回应我的行动:

<h:form>
    <p:fileUpload mode="simple" fileUploadListener="#{ADD.uploadImage}" auto="true"/>
</h:form>

这是支持bean:

@ManagedBean
@ViewScoped
public class TestClass {

    public void uploadImage(FileUploadEvent e){
        System.out.println("EVENT");
    }

}

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>51200</param-value>
        </init-param>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>/tmpDir3/</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

问题是方法uploadImage没有被调用。

我的代码有什么问题或缺少什么?

1 个答案:

答案 0 :(得分:0)

如果添加@ManagedBean注释,则bean实例的名称将是您的类的名称(带小写的第一个字母)。在您的示例中,由于您的类的名称为TestClass,因此您的bean的名称将为testClass

因此,当您编写一个应该在此bean上调用方法的EL表达式时,您必须编写#{testClass.myMethod}(在您的情况下,这将是#{testClass.uploadImage}

请参阅:http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html