Primefaces <p:fileUpload 没有调用 bean 方法

Primefaces <p:fileUpload isn't invoking the bean method

我正在尝试实现 primefaces 文件上传,但没有调用 bean 方法,顺便说一句,我正在使用 spring 框架和 prettyfaces:

人脸-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <resource-bundle>
            <base-name>label</base-name>
            <var>msg</var>
        </resource-bundle>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

web.xml 文件:

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>log4j-config-location</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>co.com.core.commons.LogContextListener</listener-class>
    </listener>
    <!-- ############################################# -->
    <!-- # File upload                                      # -->
    <!-- ############################################# -->
    <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>2097152</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <!-- ############################################# -->
    <!-- # QUARTZ                                    # -->
    <!-- ############################################# -->
    <!-- listener>
        <listener-class>
            org.quartz.ee.servlet.QuartzInitializerListener
        </listener-class>
    </listener-->
    <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>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

.xhtml代码:

<h:form enctype="multipart/form-data" prependId="false">
                            <p:fileUpload mode="simple" value="#{templateController.file}" />
                            <p:commandButton value="Upload" actionListener="#{templateController.upload}" ajax="false" />
                        </h:form>

bean方法:

public void upload() {  
    FacesMessage msg = new FacesMessage("Success! is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

我尝试将 FORWARD 添加到过滤器中,因为我也在使用漂亮的面孔,但仍然无法正常工作,谢谢。

尝试执行以下操作,

将此方法添加到您的 bean,

    public void handleProfileFileUpload(FileUploadEvent event) {
    if (event != null) {


        try {
            InputStream inputStream = event.getFile().getInputstream();
            // Set inputstream to your object
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

移除 CommandButton 并修改 fileUpload 配置,fileUploadListener 处理上传

<p:fileUpload fileUploadListener="#{<your bean>.handleProfileFileUpload}"
                            mode="advanced" sizeLimit="2097152"
                            allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

好吧,经过大量阅读,我终于解决了这个问题,在我的例子中,解决方案是将上下文参数 (primefaces.UPLOADER) 添加到 web.xml 文件中(检查 this请回答),这个文件看起来像这样,也可能有助于将项目阶段设置为开发(在此文件中注释),这可以为您提供一些与调试过程相关的附加信息:

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>log4j-config-location</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>co.com.core.commons.LogContextListener</listener-class>
    </listener>
    <!-- ############################################# -->
    <!-- # File upload                                      # -->
    <!-- ############################################# -->
    <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>2097152</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>

    <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>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

如果您使用 Spring MVC,您的过滤器映射应指向 Spring DispatcherServlet 而不是 Faces Servet。

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Spring MVC Servlet</servlet-name>
</filter-mapping>