错误代码:
警告: javax.servlet.ServletException: The request content-type is not a multipart/form-data
javax.faces.FacesException: javax.servlet.ServletException: The request content-type is not a multipart/form-data
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:836)
...
错误解决方法一:
1.<h:form enctype="multipart/form-data"> 这个要指定
2.<p:commandButton action="#{tcAdvertController.create()}" update=":forupdate,:msg" oncomplete="adds.hide();" value="保存" ajax="false">要指定ajax="false" 否则ajax的默认值是true
部分代码贴出如下:
<p:dialog header="添加" widgetVar="adds" modal="true" appendTo="@(body)" position="center" width="700"> <h:form id="add" enctype="multipart/form-data"> <h:panelGrid columns="2"> <h:outputLabel value="广告跳转地址:"/> <h:inputText value="#{tcAdvertController.selected.adUrl}" required="true" requiredMessage="请填写广告跳转地址"/> <h:outputLabel value="是否显示:"/> <h:selectOneMenu value="#{tcAdvertController.selected.isshow}"> <f:selectItem itemLabel="是" itemValue="1"/> <f:selectItem itemLabel="否" itemValue="0"/> </h:selectOneMenu> <h:outputLabel value="类型:"/> <h:selectOneMenu value="#{tcAdvertController.selected.type}"> <f:selectItem itemLabel="首页" itemValue="0"/> <f:selectItem itemLabel="我要服务" itemValue="1"/> </h:selectOneMenu> <h:outputLabel value="图片:"/> </h:panelGrid> <p:fileUpload allowTypes="/(\.|\/)(gif|jpe?g|png)$/" update="addimage,:msg" style=" 560px;" multiple="true" uploadLabel="上传" invalidFileMessage="图片文件有误,格式:gif、jpg、jpeg、png" sizeLimit="2097152" invalidSizeMessage="图片文件不能大于2M" cancelLabel="取消上传" label="选择上传图片" fileUploadListener="#{tcAdvertController.handleFileUpload}"> </p:fileUpload> <h:panelGroup id="addimage"> <ui:repeat var="item" value="#{tcAdvertController.imgesList}"> <div style="float: left;border:solid 1px aqua;" > <div style="padding:10px;text-align: center"> <p:commandButton style="background: url('../resources/images/del.png'); 35px;height: 35px;" value="" action="#{tcAdvertController.delImage(item)}"/> <p/> <p:lightBox width="400" height="auto"> <h:outputLink style="text-decoration:none;" value="#{item}" > <h:graphicImage style=" 50px;height: 50px;" value="#{item}" alt="点击“查看/刷新”"/> </h:outputLink> </p:lightBox> </div> </div> </ui:repeat> </h:panelGroup> <br /> <div style="clear: both"> </div> <p/> <div> <p:commandButton action="#{tcAdvertController.create()}" update=":forupdate,:msg" oncomplete="adds.hide();" value="保存" ajax="false"></p:commandButton> <p:commandButton oncomplete="adds.hide();" value="#{bundle.Close}" /> </div> </h:form> </p:dialog>
没有以上两步的话就会报出javax.servlet.ServletException: The request content-type is not a multipart/form-data这个错误
解决方法二:可能是primefaces的jar包版本问题,我做的项目jar包本来是primefaces-4.0.jar 后来改成了更高版本的就可以了
另外,好像是版本超过5以后的primefaces的jar包,就要更换下列书写方式:
<p:commandButton update=":forupdate,:msg" oncomplete="adds.show();" value="保存" /> 要改成oncomplete="PF('adds').show();"
其中,adds是一个<p:dialog>的元素