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);
}
}
}