主要面孔上传

时间:2011-11-24 00:54:27

标签: file-upload jsf-2 primefaces

我在使用primefaces上传小部件上传时遇到问题。经过一番调查后,我注意到它发送了错误的帖子类型“url-data-encoded”而不是multi-patrt请求。即使我的表格明确声明:

p:dialog name= "upload" id="mapping" header="Upload Mapping File" widgetVar="mappingFileDialog" fixedCenter="true">
     <h:form prependId="true" id="uploadMapping" enctype="multipart/form-data">
                    <p:fileUpload value="#{panaceaController.file}" mode="simple"/>
                    <p:commandButton id="uploadbuttom" value="Submit" ajax="true" 
            action="#{panaceaController.handleFileUpload}"/>
     </h:form>
    </p:dialog>

控制器中的文件字段为空,会发生什么。 我猜其他相关的事情是我在这个页面中有多个表单,我猜Primefaces提交了错误的表单。

顺便说一句,如果我为上传表单禁用了ajax,请求的enctype是正确的,但它不再调用该操作了吗?!

这是我的html表单:

 

<h:form prependId="false">
    <p:dataTable id="instances" lazy="false" paginator="false" var="instance"
                 value="#{panaceaController.instances}">
        <p:column>
            <f:facet name="header">
                <h:outputText value="Name"/>
            </f:facet>
                <h:outputText value="#{instance.name}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Directory"/>
            </f:facet>
            <h:outputText value="#{instance.directory}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                Options
            </f:facet>
            <p:commandLink async="true" update="propertiesTable" oncomplete="propertiesDialog.show();">
                <h:graphicImage value="img/properties.png" />
                <f:setPropertyActionListener value="#{instance}" target="#{panaceaController.instance}"/>
            </p:commandLink>

            <p:commandLink async="true"  onclick="confirmDelete.show();" >
                <h:graphicImage value="img/edit-delete.png"/>
                <f:setPropertyActionListener value="#{instance}" target="#{panaceaController.instance}"/>
            </p:commandLink>
            <p:commandLink async="true"  onclick="mappingFileDialog.show();" >
                <h:graphicImage value="img/mapping.png"/>
                <f:setPropertyActionListener value="#{instance}" target="#{panaceaController.instance}"/>
            </p:commandLink>

        </p:column>

    </p:dataTable>
</h:form>
<p:dialog id="properties" header="Properties"  widgetVar="propertiesDialog" fixedCenter="true">
    <h:panelGroup id="propertiesTable" >
        <p>
        Properties for #{panaceaController.instance.directory} :
        </p>
    <h:form>
    <p:dataTable id="propertyTable" var="propertyInstance" value="#{panaceaController.instance.properties}" paginator="false">
        <p:column>
            <h:outputText value="#{propertyInstance.name}"/>
        </p:column>
        <p:column>
            <h:inputText value = "#{propertyInstance.value}" required="true"/>
        </p:column>
    </p:dataTable>
        <h:commandButton action="#{panaceaController.saveProperties}" value="Save"/>
    </h:form>
    </h:panelGroup>
</p:dialog>
<h:form>
<p:confirmDialog widgetVar="confirmDelete" message="Are you sure you want to delete this Panacea instance?"
                                         severity="warn">
      <p:commandButton value="Yes" update="instances" oncomplete="confirmDelete.hide()"
            actionListener="#{panaceaController.delete}"  />
      <p:commandButton value="Not" onclick="confirmDelete.hide()" type="button"  />
</p:confirmDialog>
</h:form>
<p:dialog name= "upload" id="mapping" header="Upload Mapping File" widgetVar="mappingFileDialog" fixedCenter="true">
 <h:form prependId="true" id="uploadMapping" enctype="multipart/form-data">
                <p:fileUpload value="#{panaceaController.file}" mode="simple"/>
                <p:commandButton id="uploadbuttom" value="Submit" ajax="false" async="false"
        action="#{panaceaController.handleFileUpload}"/>
 </h:form>
</p:dialog>

1 个答案:

答案 0 :(得分:2)

  

我没有在web.xml中设置任何过滤器

您需要根据PrimeFaces User Guideweb.xml中配置PrimeFaces文件上传过滤器。以下是3.0.M4用户指南第170页的摘录:

  

3.34 FileUpload

     

...

     

FileUpload入门

     

首先要做的是配置解析multipart请求的fileupload过滤器。 FileUpload过滤器应该映射到Faces Servlet。

<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>

同样适用于像M1这样的旧版beta版本。