Primefaces - FileUploader不调用ManagedBean

时间:2013-04-15 12:23:58

标签: java jsf file-upload primefaces

我想实现primefaces File uploader

我的实施:

<h:form enctype="multipart/form-data">
                                    <p:fileUpload id="fileUpload"
                                        fileUploadListener="#{fileUploadController.handleFileUpload}"
                                        mode="advanced" update="messages" sizeLimit="100000"
                                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

                                    <p:growl id="messages" showDetail="true" />

我在web.xml文件中配置了我的过滤器:

<!-- ############################################# -->
    <!-- # File upload                                      # -->
    <!-- ############################################# -->
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>
            org.primefaces.webapp.filter.FileUploadFilter
        </filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>6400</param-value>
        </init-param>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>/resources/uploads</param-value>
        </init-param>
    </filter>

  <filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>

在我的pom.xml中:

  • commons-fileupload
  • 公地IO

我的文件上传控制器如下所示:

@Component
@Scope("session")
public class FileUploadController {

    private int size = 1024;

    /**
     * log4j
     */
    private static Logger log = LogManager.getLogger(FileUploadController.class);

    public void handleFileUpload(FileUploadEvent event) {  
        log.info("Method handleFileUpload invoked");
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
        try {
            File targetFolder = new File("/resources/uploads/images");
            InputStream inputStream = event.getFile().getInputstream();
            OutputStream out = new FileOutputStream(new File(targetFolder,
                    event.getFile().getFileName()));
            int read = 0;
            byte[] bytes = new byte[size];
            log.info("read file stream");
            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            inputStream.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            log.error(e);
            e.printStackTrace();
        }

    }

我的问题是,我没有从控制器获取日志消息,上传我的文件?

我非常感谢你的回答!!!

btw我的PF版本是3.5

更新

我的模板看起来就是这样:

</h:head>
<h:body>
    <div class="well" style="margin-top: 50px">
        <div class="container">

            <h:form>
                <p:growl id="growl" sticky="true" showDetail="true" />

                <p:wizard widgetVar="wiz"
                    flowListener="#{productWizardHandler.onFlowProcess}">

                    <!-- Basic Product Tab -->
                    <p:tab id="basicProduct" title="BasicProduct">

                        <p:panel header="Fill in your basic Product Information">

                            <h:messages errorClass="error" />
                            <h:panelGrid columns="2" columnClasses="label, value"
                                styleClass="grid">
                                <h:outputText value="Product Name: *" />
                                <p:inputText required="true" label="Product Name"
                                    value="#{productService.instance.productName}" />

                                <h:outputText value="Categorie: *" />
                                <p:inputText label="Categorie" value="#" />

                                <h:outputText value="Description: *" />
                                <p:inputTextarea required="true"
                                    value="#{productService.instance.description}" />

<!-- Select your product  -->
<!--                                <h:outputLabel value="Select the product you want to create:" for="dd" />
                                <p:autoComplete id="dd" dropdown="true"
                                    value="#{autoCompleteBean.txt6}"
                                    completeMethod="#{autoCompleteBean.complete}" />
 -->
                            </h:panelGrid>

                        </p:panel>
                    </p:tab>

                    <!-- Image Tab -->
                    <p:tab id="image" title="Image">
                        <p:panel header="Upload your Product Image Details">

                            <h:messages errorClass="error" />

                            <h:panelGrid columns="2" columnClasses="label, value"
                                styleClass="grid">
                                <h:form enctype="multipart/form-data">

                                    <p:fileUpload id="fileUpload"
                                        fileUploadListener="#{fileUploadController.handleFileUpload}"
                                        mode="advanced" update="messages" sizeLimit="100000"
                                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

                                    <p:growl id="messages" showDetail="true" />

                                </h:form>
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>

                    <!-- Special Tab -->

                    <p:tab id="speziel" title="Spezial Tab">

                        <!-- ################################################################################ -->
                        <p:panel>
                            <ui:include src="product_templates/test.xhtml" />
                            <ui:include src="product_templates/clothes.xhtml" />
                            <ui:include src="product_templates/arbnb.xhtml" />

                        </p:panel>
                    </p:tab>

                    <!-- Summary -->
                    <p:tab id="summary" title="Summary">
                        <p:panel header="Product Summary">

                            <h:panelGrid id="productSummary" columns="6">
                                <h:outputText value="Product Name: " />
                                <h:outputText styleClass="outputLabel"
                                    value="#{productWizardHandler.product.productName}" />

                                <h:outputText value="Categorie: " />
                                <h:outputText styleClass="outputLabel" value="#" />

                                <h:outputText value="Description: " />
                                <h:outputText styleClass="outputLabel"
                                    value="#{productWizardHandler.product.description}" />>  

                      INSERT outputs für speziell products
                                                </h:panelGrid>

                            <p:commandButton value="Submit" update="growl"
                                actionListener="#{productWizardHandler.save}" />

                        </p:panel>
                    </p:tab>

                </p:wizard>

            </h:form>
        </div>
    </div>
</h:body>

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您没有为primefaces文件上传过滤器

指定过滤器映射
  <filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>