0

I'm trying to get a really simple example I found online to work, I've tried searching everywhere but can't seem to find a solution.

This is the bean that I'm using:

@Named("uploadBean")
@RequestScoped
public class UploadBean {
    private Part file;
    private String fileContent;

    public void upload() {
        try {
            fileContent = new Scanner(file.getInputStream())
                    .useDelimiter("\\A").next();
        } catch (IOException e) {
            // Error handling
        }

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Uploaded!"));
    }

    public Part getFile() {
        return file;
    }

    public void setFile(Part file) {
        this.file = file;
    }

    public String getFileContent() {
        return fileContent;
    }

    public void setFileContent(String fileContent) {
        this.fileContent = fileContent;
    }
}

This is the form that uses it:

<h:form id="file-upload-test" enctype="multipart/form-data">
    <h:inputFile id="in-file" value="#{uploadBean.file}"/>
    <h:commandButton value="Upload!" action="#{uploadBean.upload}"/>
</h:form>

Whenever I try to upload a file, upload() is never called.

Maybe the answer is too obvious, but I'm using JSF 2.2.7 and I'm not sure why this simple example shouldn't work?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Which server? Does it support CDI? Which faces-config.xml version have you declared? Which web.xml version have you declared? What if you use a regular non-upload form? How does the HTTP request payload look like? Have you tried latest Mojarra version (currently 2.2.12)? – BalusC Jul 26 '15 at 05:53
  • I'm using Glassfish 4. Faces-config.xml is version 2.2, and web.xml is version 3.1. A regular form works fine and calls the same method properly. The HTTP request payload shows the file in the request. I updated the Glassfish JSF to 2.2.12 form 2.2.7 (and verified pragmatically), but still doesn't work. :( – mohoromitch Jul 26 '15 at 16:45
  • 1
    Do you have any filters in the web application which might consume the request body (e.g. via `request.getParameter()`) before it hits the `FacesServlet`? – BalusC Jul 26 '15 at 16:50
  • That was it. I completely forgot that I had PrettyFaces filtering the page. Thanks so much for your help! – mohoromitch Jul 26 '15 at 22:26

0 Answers0