0

我已经发布了一个关于几天前的问题。p:fileupload在深入研究这个问题后,我意识到我的主要问题似乎来自不同的起源,因此我提出了一个新问题。

我正在为移动设备开发一个网络应用程序(JSF2.2),用户可以在其中多次上传文件。我想使用 Primefaces 的p:fileupload组件来完成这项似乎不起作用的工作。我发现问题似乎enctype="multipart/form-data"<h:form>. 每当我的表单中有此属性时,commandButton 都不会调用其action=""属性中指定的 bean 方法(使用时也不会actionListener="")。当我删除该enctype="multipart/form-data"属性时,除了文件上传之外,一切都正常工作。

出于测试和调试目的,我创建了一个uploadTest.xhtml当前看起来如下的页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"         
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <f:view renderKitId="PRIMEFACES_MOBILE" />

    <h:head>
    </h:head>

    <h:body>
        <h:form id="dasistmeineform" enctype="multipart/form-data">
            <p:fileUpload value="#{uploadBean.uploadedFile}" mode="simple"/>
            <p:commandButton action="#{uploadBean.upload}" ajax="false" value="upload" />
        </h:form>
    </h:body>

</html>

为了从可能的错误原因列表中排除 Primefaces,我还创建了另一个测试页面,其中我只使用<h:inputFile>并且<h:commandButton>没有 primefaces 组件。这给了我同样的结果。

这是我的 UploadBean.java:

import javax.faces.bean.RequestScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.UploadedFile;

@ManagedBean
@RequestSoped
public class UploadBean {

    private UploadedFile uploadedFile;

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public void upload() {
        if (this.getUploadedFile() != null) {
            System.out.println("Hurray!");
        }
    }



}

我个人的猜测是,这个问题与应用程序的设置方式或 JSF 在应用程序中的实现方式有关。为了解决这个问题,我遵循了@BalusC 的JSF 设置指南。此外,我遵循了他的一些其他故障排除指南,包括如何排除 p:fileupload 故障以及按下 h:commandButton 时未调用 bean 方法的可能原因列表。不幸的是,到目前为止没有任何帮助。

我在 Netbeans 控制台和 Chrome 的开发人员控制台中都没有收到错误。按下上传按钮后,我可以使用开发人员工具看到 HTTP 请求。响应的代码为 200,所以一切都应该在 HTTP 端正常工作,对吧?

Web 应用程序使用以下组件:

JSF 2.2.13 (莫哈拉)

春季网络流

春季MVC

Primefaces 5.3

全方位 1.3

靴面 0.9.1

该应用程序直接通过 Netbeans 在 Jetty 8.1.5 服务器上部署和测试。

为了检查我是否下载了所有必要的依赖项,我将包含我的 pom.xml 的相关方(我使用 maven):

 <dependencies>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.faces</artifactId>
            <version>2.2.13</version>
        </dependency>


        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>

         <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>
</dependencies>

我包括commons-iocommons-fileupload测试是否p:fileupload可以使用旧的(在 Servlet 3.0 之前)方式,但它没有。

这是我的 web.xml :(请注意,我也在没有成功的情况下测试了context-paramandfilter条目p:fileupload):

-->
<web-app 
    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"
    version="3.0">

    <description>Webflow Archetype</description>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>auto</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring/applicationContext.xml,/WEB-INF/config/web-application-config.xml
        </param-value>
    </context-param>
    <!-- Causes Facelets to refresh templates during development -->
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.expressionFactory</param-name>
        <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
    </context-param>

    <context-param>
        <param-name>com.sun.faces.sendPoweredByHeader</param-name>
        <param-value>false</param-value>
    </context-param>

    <!-- Disable/Enable PF/M default theme:   -->
    <!--    <context-param>
        <param-name>primefaces.mobile.THEME</param-name>
        <param-value>none</param-value>
    </context-param>-->

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>cupertino</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.FONT_AWESOME</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <description>Define partial state saving as true/false.</description>
        <param-name>javax.faces.PARTIAL_STATE_SAVING_METHOD</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- Custom MarqueeComponent -->
    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/marquee-taglib.xml</param-value>
    </context-param>
    <context-param>
        <param-name>BootsFaces_USETHEME</param-name>
        <param-value>true</param-value>
    </context-param>

     <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value/>
        </init-param>
        <load-on-startup>2</load-on-startup>
        <!--        <multipart-config>
            <max-file-size>10485760</max-file-size>
            <max-request-size>20971520</max-request-size>
            <file-size-threshold>5242880</file-size-threshold>
        </multipart-config>-->
    </servlet>
    <!-- Map all /spring requests to the Dispatcher Servlet for handling -->
    <servlet>
        <servlet-name>Jersey Spring Servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>at.managementpartners.icosysmobile.rest</param-value>
        </init-param>
    </servlet>



    <servlet>
        <servlet-name>FileServlet</servlet-name>
        <servlet-class>at.managementpartners.icosysmobile.web.util.FileServlet</servlet-class>
    </servlet>



    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/spring/ *</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>at.managementpartners.icosysmobile.web.util.ImageServlet</servlet-class>
    </servlet>
    <!-- Serves static resource content from .jar files such as spring-faces.jar -->
    <!-- Map all /resources requests to the Resource Servlet for handling -->
    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup> 
        <!--        <multipart-config>
            <max-file-size>10485760</max-file-size>
            <max-request-size>20971520</max-request-size>
            <file-size-threshold>5242880</file-size-threshold>
        </multipart-config>-->
    </servlet>
    <!-- Faces Servlet Mapping -->
     <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/ *</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/javax.faces.resource/ *</url-pattern>
    </servlet-mapping>

    <!--   
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

        <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>/do/ *</url-pattern>
        </servlet-mapping>
    -->    

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>


    <!-- servlet>
        <servlet-name>Primefaces Resource Servlet</servlet-name>
        <servlet-class>
            org.primefaces.resource.ResourceServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Primefaces Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping -->
    <!-- Welcome files -->
    <servlet-mapping>
        <servlet-name>Jersey Spring Servlet</servlet-name>
        <url-pattern>/rest</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Jersey Spring Servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <!--<servlet-mapping>
        <servlet-name>GetClientHostnameServlet</servlet-name>        
        <url-pattern>/GetClientHostnameServlet</url-pattern>
    </servlet-mapping> -->

    <servlet-mapping>
        <servlet-name>FileServlet</servlet-name>
        <url-pattern>/files/*</url-pattern>
    </servlet-mapping>

    <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>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>ClickjackFilterSameOrigin</filter-name>
        <filter-class>at.managementpartners.icosysmobile.web.util.ClickjackFilter</filter-class>
        <init-param>
            <param-name>mode</param-name>
            <param-value>SAMEORIGIN</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>ClickjackFilterSameOrigin</filter-name>
        <url-pattern>/ *</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>charEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>charEncodingFilter</filter-name>
        <url-pattern>/ *</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Allows Jetty to serve Faces applications -->
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

    <!--    <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 Dispatcher Servlet</servlet-name>
    </filter-mapping>-->

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/ *</url-pattern>
    </filter-mapping>


    <!-- Lebenszeit der Session in Minuten (480 min = 8 Stunden; 180 min = 3 Stunden)    -->
    <session-config>
        <session-timeout>180</session-timeout>
        <cookie-config>
            <secure>true</secure>
        </cookie-config>
    </session-config>

    <mime-mapping>
        <extension>ttf</extension>
        <mime-type>application/x-font-ttf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>woff</extension>
        <mime-type>application/x-font-woff</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>woff2</extension>
        <mime-type>application/font-woff2</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>eot</extension>
        <mime-type>application/vnd.ms-fontobject</mime-type>
    </mime-mapping>
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->



    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <trim-directive-whitespaces>false</trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>

    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/errors/error.xhtml</location>
    </error-page>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/errors/error.xhtml</location>
    </error-page>

    <error-page>
        <error-code>403</error-code>
        <location>/WEB-INF/errors/error.xhtml</location>
    </error-page>

    <error-page>
        <error-code>402</error-code>
        <location>/WEB-INF/errors/error.xhtml</location>
    </error-page>

    <error-page>
        <error-code>401</error-code>
        <location>/WEB-INF/errors/error.xhtml</location>
    </error-page>

    <error-page>
        <error-code>400</error-code>
        <location>/WEB-INF/errors/error.xhtml</location>
    </error-page>

</web-app>

请注意,我必须用空格 (´/ *´) 分隔“/*”,以便我的其余代码不会被格式化为注释。(如果有更聪明的方法可以做到这一点,请不要犹豫告诉我)

Spring 的 MVC Dispatcher Servlet 会以某种方式干扰 Servlet 3.0 的上传功能吗?

我是否使用了其他违反 Servlet 3.0 文件上传标准行为的组件?

我的项目是否设置正确?

非常感谢。即使是一点提示也将不胜感激。

4

0 回答 0