我正在尝试将文本文档上传到我的服务器,这让我疯狂了几天。基本上,在文件上传时假设被触发的方法#{bookController.handleFileUpload} 不会触发,但我没有收到任何错误,并且在前端文件看起来像是已上传。我假设文件在到达我的控制器方法的途中被消耗掉了。这是我的 web.xml
<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>
</filter-mapping>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>south-street</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>*.faces</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/jpmorgan/*</url-pattern>
</servlet-mapping>
<context-param>
我在我的 pom.xml 中添加了必要的 jar(common-io.jar 和 common-fileupload.jar)。这是我假设处理文件的页面:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/layouts/no-ajax.xhtml">
<ui:define name="head">
</ui:define>
<ui:define name="title">#{msg['add_book']}</ui:define>
<ui:define name="content">
<h:form id="form" enctype="multipart/form-data">
<p:messages id="messages" autoUpdate="true"/>
<p:panelGrid id="grid">
<p:row> <p:column>
<h:outputLabel for="name" value="#{msg['label_book_title']}" />
<p:spacer width="100" height="10" />
<p:inputText id="name" value="#{bookBean.book.title}"/>
</p:column> </p:row>
<p:row> <p:column>
<p:fileUpload id="upload" value="#{bookBean.file}"
dragDropSupport="true"
update="messages"
fileUploadListener="#{bookController.handleFileUpload}" />
</p:column> </p:row>
</p:panelGrid>
<p:toolbar>
<p:toolbarGroup align="left">
<p:commandButton value="#{msg['button_next']}" action="next" execute="@form" update="@form"/>
<p:commandButton value="#{msg['button_cancel']}" action="exit" execute="@form" update="@form"/>
</p:toolbarGroup>
</p:toolbar>
</h:form>
</ui:define>
<ui:define name="help">
<h:outputText value="#{msg['help_add_page']}" />
</ui:define>
</ui:composition>
以上页面的布局:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:sec="http://www.springframework.org/security/tags"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>#{msg['application_name']}</title>
<link rel="icon" href="resources/resources/images/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="resources/resources/images/favicon.ico" />
<link rel="stylesheet" href="resources/resources/styles/blueprint/screen.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="resources/resources/styles/blueprint/print.css" type="text/css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" href="${request.servletPath}/styles/blueprint/ie.css" type="text/css" media="screen, projection" />
<![endif]-->
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit id="top" position="north" header="#{msg['application_name']}" size="100" collapsible="true" collapsed="false" resizable="true">
<ui:insert name="title" />
</p:layoutUnit>
<p:layoutUnit id="west" position="west" size="250" header="#{msg['label_help']}" resizable="true" collapsible="true" collapsed="false">
<ui:insert name="help" />
</p:layoutUnit>
<p:layoutUnit id="center" position="center" >
<ui:insert name="content" />
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
有谁知道我做错了什么?非常感谢。