在我的项目中,我有一个页面,其中有一个名为“Create Package”的按钮可以打开一个对话框。在对话框中,用户可以上传一个或多个文件,如果他犯了错误,他可以删除以前关联的文件。
我的p:upload组件存在一些问题,我将在下面列出。
1)如果我在没有enctype =“multipart / form-data”参数的情况下指定h:form,文件关联工作正常,但在尝试删除文件时出现以下错误:
WARNING: javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded
javax.faces.FacesException: javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:44)
at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
...
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/sism_webapp] threw exception [org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is application/x-www-form-urlencoded
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:806)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:261)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:285)
at org.apache.catalina.connector.Request.parseParts(Request.java:2722)
2)仅添加enctype =“multipart / form-data”属性时会出现相同的行为。
3)如果我将以下属性添加到删除按钮:
onclick="$('#frm_creaDocumenti\\:fu_upload').('disabled','disabled');"
删除操作正确执行,但对话框会自动关闭。
是否有解决方案来执行文档删除并保持对话框打开?
环境:Primefaces 4.0,Tomcat 7.0.40
XHTML页面
<p:growl id="growl" showDetails="true" />
<p:commandButton id="btn_crea" value="CREA"
onclick="PF('creaPackage').show()" type="button" />
<p:dialog id="dlg_crea" header="Crea Package"
widgetVar="creaPackage" width="95%" position="top"
modal="true" hideEffect="fade" dynamic="true">
<!-- enctype="multipart/form-data" -->
<h:form id="frm_creaDocumenti">
<p:fileUpload id="fu_upload" value="#{packageBean.fileUpload}"
widgetVar="wv_upload"
uploadLabel="ASSOCIA" label="SCEGLI FILE" cancelLabel="ANNULLA SCELTA"
update="@form :growl"
fileUploadListener="#{packageBean.associaDocumentoCrea}"
process="@form" mode="advanced" sizeLimit="1000000">
</p:fileUpload>
<!-- onclick="$('#frm_creaDocumenti\\:fu_upload').('disabled','disabled');" -->
<p:commandButton id="deleteDocumentButton"
ajax="true" value="Elimina"
actionListener="#{packageBean.eliminaDocumenti('CREA')}"
update=":growl" />
<p:dataTable id="documentTable" var="doc" value="#{packageBean.documenti}" rowKey="#{doc.id}"
scrollable="true" scrollHeight="270"
selection="#{packageBean.documentiSelezionati}" selectionMode="multiple">
<p:ajax event="rowSelect" listener="#{packageBean.onRowSelect}" update="@this" />
<p:column headerText="Codice">#{doc.codice}</p:column>
<p:column headerText="Titolo">#{doc.titolo}</p:column>
<p:column headerText="Autore">#{doc.autore}</p:column>
</p:dataTable>
</h:form>
</p:dialog>
Managed Bean
public void associaDocumentoCrea(FileUploadEvent event)
{
// call "write file" procedure...
}
public void eliminaDocumenti(String azione)
{
// call "delete file" procedure...
}
的web.xml
<!--
According to the Primefaces User Guide, I do not need to set filter
and filter-mapping for the file upload if I use "auto"
-->
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto</param-value>
</context-param>
的pom.xml
<!--
I added the following Maven dependencies to let the file upload
component work
-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>