【问题标题】:Primefaces <p:fileUpload isn't invoking the bean methodPrimefaces <p:fileUpload 没有调用 bean 方法
【发布时间】:2016-06-14 11:04:24
【问题描述】:

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

faces-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的范围是什么?
  • 它有会话范围

标签: spring file-upload primefaces web.xml prettyfaces


【解决方案1】:

尝试执行以下操作,

将此方法添加到您的 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 config类似这样,fileUploadListener处理上传

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

【讨论】:

  • handleProfileFileUpload 方法中的事件对象调试中你能看到什么?
  • 其实不多,在 Web 浏览器控制台中也没有任何 js 错误
  • 那你需要调试看看,不然找不到线索
  • 我朋友就是这么做的,但是我没有收到任何错误,不知道我还能做什么。
【解决方案2】:

最后经过大量阅读后我解决了这个问题,在我的情况下,解决方案是在 web.xml 文件中添加一个上下文参数(primefaces.UPLOADER)(请检查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>

【讨论】:

    【解决方案3】:

    如果您使用 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>
    

    【讨论】:

    • 你的意思是Spring MVC? OP没有使用它。 Afaik,同时使用 JSF 和 MVC 是一个糟糕的选择(如果可以工作,请参阅 stackoverflow.com/questions/18744910/…
    • 我使用 Spring MVC / Webflow 以及 Primefaces 和 JPA 没有问题。我不认为这是一个糟糕的选择当你说“OP 没有使用那个”时你是什么意思?
    • 您使用的是 Spring MVC 还是 Spring Webflow?如果我仔细阅读我发布的链接,可以使用两种不同的技术。您可以将 Spring Webflow 与 JSF 一起使用,但不能使用 Spring MVC(尽管如果我阅读正确,您可以并排运行它们)。因此,如果您实际上做的不止于此,如果有一些可公开访问的代码以查看它的实际使用方式并可能更正我发布的链接,我会非常感兴趣
    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 2012-04-15
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    相关资源
    最近更新 更多