user1924104
user1924104

Reputation: 901

handleFileUpload not called

Having a weird issue with my file upload

handleFileUpload is never called, and in the xhtml file it underlines this with yellow and says unkown property

here is my web.xml

<context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <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>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>

my file upload controller, i have gone back to see if i can call the most basic version, but currently it is not

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

@ManagedBean(name = "fileUploadController")
public class FileUploadController {


     public void handleFileUpload(FileUploadEvent event) {
     System.out.println("called");
     FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
     FacesContext.getCurrentInstance().addMessage(null, msg);
     }
     }
      /*
    private String username;

    @PostConstruct
    public void init() {
        System.out.println("called get username");
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
    File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution) 

    public File getDirectory(String destination, String username) {
             System.out.println("called get directory");
        // currently not working, is not calling the username or destination 
        //set the user directory from the destinarion and the logged user name
        File directory = new File(destination, username);
        //check if the location exists
        if (!directory.exists()) {
            //let's try to create it
            try {
                directory.mkdir();
            } catch (SecurityException secEx) {
                //handle the exception
                secEx.printStackTrace(System.out);
                directory = null;
            }
        }
        return directory;
    }

    public void handleFileUpload(FileUploadEvent event) {
             System.out.println("called handle file");
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            //handle the exception
            e.printStackTrace();
        }
    }

    public void copyFile(String fileName, InputStream in) {
        try {


            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(theFile + "/" + fileName)); // cannot find path when adding username atm
            System.out.println(theFile); //testing 

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
*/

here is my uploadText.xhtml

<?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:corejsf="http://corejsf.com"> 
    <h:outputStylesheet name="css/testcss.css" />
    <h:head>
        <title>Print to Uni</title>
        <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
        <!-- <link rel="stylesheet" type="text/css" href="style.css" title="style" /> -->
    </h:head>
    <body>
        <div id="main">
            <div id="header">

                <div id="logo">
                    <div id="logo_text">
                        <h1><a href="/GUI/index.xhtml">Remote<span class="logo_colour">Printing</span></a></h1>
                        <h2>This is a web app that allows users to print to University</h2>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <div id="site_content">
        <div id="content">
            <h:body>
                <h:form enctype="multipart/form-data">
                    Upload a text file:
                    <h:form>
                        <!--<p:growl id="messages" showSummary="true" showDetail="true" /> -->
                        <br></br>
                        <br></br>

                        <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                                      mode="advanced" 
                                      update="messages"
                                      sizeLimit="100000000" 
                                      allowTypes="/(\.|\/)(gif|jpe?g|png|doc|docx|txt|pdf)$/"
                                      auto="true"/>
                        <p:growl id="messages" showDetail="true"/>
                    </h:form>
                    <br></br>
                    <br></br>
                    <p:commandButton value="Submit" action="/GUI/submittedText" icon ="ui-icon-arrowstop-1-n"/>
                    <div class="divider"/>
                    <p:commandButton  value="Homepage" action="#{userBean.buttonHome}" icon="ui-icon-home"/>   
                </h:form>
            </h:body>
        </div>
    </div>
</html>

i have both commons files that are needed in my libraries, what is causing this ? i did have it working but did not back up the changes and now it does not work

Upvotes: 0

Views: 2084

Answers (1)

BalusC
BalusC

Reputation: 1109875

Your HTML is syntactically invalid. Among others, you've multiple <body> tags and you're nesting <form> elements. JSF look like a magician, but if you force it to generate syntactically invalid HTML, then it can't do much for you. The browser bahavior is unspecified when it comes to syntactically invalid HTML. Having nested forms is more than often the cause that the browser doesn't submit the expected data to the server and thus the server end up receiving nothing.

Your HTML should have only one <body> tag (in JSF terms, that's thus only one <h:body>). Your HTML should also not have nested <form> elements (in JSF terms, you should never nest <h:form> in each other). Open the page in browser, rightclick and View Source. Copypaste it into the aforelinked W3 validator and fix the issues individually.

Apart from that, your managed bean does not have a scope annotation assigned. It'll behave like @NoneScoped and thus be recreated everytime it's referenced in EL, hereby causing the model properties and actions to be set and invoked in completely separate instances. Make it at least @ViewScoped.

Upvotes: 3

Related Questions