Null in Primefaces Component Uploadfile

Asked

Viewed 194 times

1

I am having trouble trying to recover that file,file, in my bean.

.xhtml

<h:form id="form" enctype="multipart/form-data">
 <p:fileUpload value="#{bean.file}" 
                    skinSimple="true" mode="simple" />
 <p:commandButton value="Enviar" ajax="true"
                    action="#{bean.addFile}" /></h:form>

Java bean.

@ManagedBean
@ViewScoped
public class Bean{

    private UploadedFile file;


    public UploadedFile getFile() {
        return file;
    }

    public void setfile(UploadedFile file) {
        this.file = file;
    }

    public void addFile() {
        try {
            String fileName = file.getFileName();
            File fileOut = new File(fileName);

            FileOutputStream fileOutputStream = new FileOutputStream(fileOut);
            fileOutputStream.write(file.getContents());
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

When trying to access the attribute file, in the method addFile(), called by commandButton, the attribute file is not set with the file I uploaded, is set to null. I can’t find the problem, I’ve searched several sources and was unsuccessful.

The Imports:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.UploadedFile;
  • Try changing the attribute ajax of commandButton for false.

  • @Henry already tried, setando ajax="false ", the method addFile() nor called is

  • Change the attribute action of commandButton for actionListener.

  • @Henry did not understand. attribute action of commandButtonfor commandButton?

1 answer

1

I see two points that need attention:

  1. The use of ajax="true" in the component commandButton when used in conjunction with the component FileUpload, where it is configured to use the simple mode (mode="simple"). This is because the component in the simple version does not support requests ajax. If you really need this support, you can use advanced mode (mode="advanced").

  2. The use of action in the component commandButton is wrong, because the action should be used when browsing between pages is intended, instead use the actionListener, which is used in cases where you need to execute a view-related logic, where there is no need for page exchange.


Here’s an example of what it would look like:

<h:form enctype="multipart/form-data">
    <p:fileUpload value="#{bean.file}" mode="simple" skinSimple="true"/>
    <p:commandButton value="Enviar" ajax="false" actionListener="#{bean.addFile}"/>
</h:form>
  • Great, I agree. I was using actionListener before, but I switched to take a test and I didn’t remember to correct

  • I’m using the ajax="false" also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.