0

I'm trying to upload a text document to my server and it drives my crazy for a couple of days. Basically the method which suppose to be fired #{bookController.handleFileUpload} when the file is uploaded does not fire, but I don't get any errors and on the front end the file looks like it is uploaded. I assume the file gets consumed somewhere on the way to my controller method. Here is my 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>

I added the necessary jars (common-io.jar & common-fileupload.jar) to my pom.xml. Here is my page that suppose to handle the file:

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

And layout for the above page:

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

Does anyone have an idea what I do wrong? Big Thanks.

Lukasz
  • 691
  • 1
  • 12
  • 30
  • Hi BalusC,I've managed to run the code in a different projects but once it's moved to mine it doesn't work. There are a few examples but the difference with mine that I use more configuration in web.xml. I also tried to make it simpler in my project, without layout.xhtml and form containing only the upload element, but it still doesn't work. – Lukasz Aug 14 '13 at 08:46
  • An I'm sure that the problem lies not in the coding part, but in the configuration part (web.xml) – Lukasz Aug 14 '13 at 10:36

2 Answers2

1

Your code seems to be correct i had the same problem you have to work with JSF 2.2 and Primefaces 5.3

my code for upload is

public void uploadPhoto(FileUploadEvent e) throws IOException {

    UploadedFile uploadedPhoto = e.getFile();

    String filePath =  "TO/THE/PATH";
    byte[] bytes = null;

    if (null != uploadedPhoto) {
        bytes = uploadedPhoto.getContents();
        String filename = FilenameUtils
                .getName(uploadedPhoto.getFileName());
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(filePath + filename)));
        stream.write(bytes);
        stream.close();
    }
    setProduitIMG(uploadedPhoto.getFileName());
    setImageSize(uploadedPhoto.getSize());

    FacesContext.getCurrentInstance().addMessage(
            "messages",
            new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Your Photo (File Name " + uploadedPhoto.getFileName()
                            + " with size " + uploadedPhoto.getSize()
                            + ")  Uploaded Successfully", ""));
}

you can use it in your managed bean with it's not required to create a new fileBean you can merge with your any managedbean Class It works fine

JSF 2.1 OR 2.0 not working in this case with spring integration you have to Upgrade your project and it will work fine

Zouhair Kasmi
  • 614
  • 2
  • 6
  • 13
0

I think the problem is in your <p:commandButton value="#{msg['button_next']}" action="next" execute="@form" update="@form"/>. File upload using AJAX is not possible, so you need to add ajax="false" to your p:commandButton and maybe you have to use an actionListener instead of an action, but on the lattern, I'm not sure. You should start trying the sample from https://www.primefaces.org/showcase/ui/file/upload/single.xhtml in your context.

Divyesh Kanzariya
  • 3,629
  • 3
  • 43
  • 44
micfra
  • 2,780
  • 1
  • 23
  • 34