0

I'm trying to upload the file(image) to my registration form without success. I followed BalusC recommendations (How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable) for upload with Primefaces 8.0 and JSF 2.2. No configurations in web.xml. My upload method is not invoking. I've tried with the listener, but...nothing happened. I cut some of Bean's code just to not bother you with code that has nothing to do with my issue. Here is my html:

                <h:form id="register">
                    <h:message for="RegisterGroupPanel" style="color:red;" />
                    <h:panelGrid columns="3" id="RegisterGroupPanel">
                        <!-- register a PostValidateEvent -->
                        <f:event listener="#{userRegisterBean.validatePassword}"
                            type="postValidate" />

                        <h:outputLabel for="username" value="Username : " />
                        <h:inputText id="username" value="#{userRegisterBean.username}"
                            required="true" requiredMessage="Please enter username" />
                        <h:message for="username" style="color: red;" />

                        <h:outputLabel for="password" value="Password : " />
                        <h:inputSecret id="password" value="#{userRegisterBean.password}"
                            required="true" requiredMessage="Please enter password" />
                        <h:message for="password" style="color: red;" />
                        <h:outputLabel for="confirmPassword" value="Confirm password : " />
                        <h:inputSecret id="confirmPassword" required="true"
                            requiredMessage="Please enter confirm password" />
                        <h:message for="confirmPassword" style="color: red;" />
                    </h:panelGrid>
                </h:form>
                
                        <h:form enctype="multipart/form-data">
                            <p:fileUpload mode="simple" value="#{userRegisterBean.uploadedFile}" skinSimple="true" />
                            <p:commandButton value="Submit" ajax="false" action="#{userRegisterBean.upload}"/>
                        </h:form>
                        <br/>
                        
                    <p:commandButton value="register" action="#{userRegisterBean.register}" />

and Bean:

@Component
@Scope("view")
public class UserRegisterBean implements Serializable {
    private static final long serialVersionUID = -6740935236874916229L;

    @Autowired
    private UserApplicationService userApplicationService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    private String username;
    private String password;

    private transient UploadedFile uploadedFile;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public void upload() {
        if (uploadedFile.getSize() > 0) {
            FacesMessage message = new FacesMessage("Succesful", uploadedFile.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, message);
        } else {
            FacesMessage message = new FacesMessage("Not Succesful", "file is not uploaded");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

}
Hasip Timurtas
  • 983
  • 2
  • 11
  • 20
Denis_Mhd
  • 1
  • 2
  • `@Component`? `@Autowired`? aren't those `Spring` annotations? – areus Jan 18 '21 at 21:29
  • Yes, they are. It is a SpringBoot 2.3 multimodule project, which includes Spring Security. – Denis_Mhd Jan 19 '21 at 05:50
  • If you are using Spring I understand you have to use the Commons FileUpload version so you are probably missing the Commons Upload filter for PF. See the PF docs. – Melloware Jan 19 '21 at 18:56
  • Аctually, it works without Commons Upload filter also didn't add any dependency, as I know we have upload filter in Primefaces. I changed the form - delete p:commandButton and add listener to p:fileUpload. Also changed the Bean's upload method(add FileUploadEvent event). I still wonder why it didn't work with action... – Denis_Mhd Jan 20 '21 at 06:25

0 Answers0