我正在使用jsf2.2 primefaces 6.0,我已经实现了上传文件和点击命令按钮确认需求的解决方案。每个用户上传文件的问题,即使未满足需求,也会导致收费存储库什么都没有 我想通过仅在用户单击命令按钮来验证需求时上传文件来纠正这一点。
这里是Xhtml代码:
<p:fileUpload
fileUploadListener="#{imageUploader.handleFileUpload}"
label="Ajouter" uploadLabel="Télécharger" cancelLabel="Annuler"
mode="advanced" auto="false" sizeLimit="10485760" multiple="true"
fileLimit="10" fileLimitMessage="Maximum 10 images"
dragDropSupport="true" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
invalidFileMessage="Format de fichier non valide" invalidSizeMessage="Taille maximum 10 MB"/>
<p:commandButton update="@form" disabled="#{not facesContext.postback or facesContext.validationFailed}"
style="background:#6FDC6F;color: #FFFFFF;float:left"
styleClass="customStyleButton"
action="#{demandeBean.doAddDemande()}" value="confirmer demande">
</p:commandButton>
这里是java Bean代码:
public class ImageUploader {
private UploadedFile uploadedFile;
@ManagedProperty(value = "#{demandeBean}")
private DemandeBean demandeBean;
public void handleFileUpload(FileUploadEvent event) {
uploadedFile = event.getFile();
String shortImagePath = UUID.randomUUID().toString() + ".jpg";
if (uploadedFile != null && uploadedFile.getContents() != null) {
try {
byte[] contents = uploadedFile.getContents();
FileOutputStream fos = new FileOutputStream(DirectoryInitilizer.IMAGE_DIR + "/" + shortImagePath);
fos.write(contents);
fos.close();
demandeBean.getImages().add(shortImagePath);
FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (Exception e) {
FacesMessage message = new FacesMessage("pb", event.getFile().getFileName() + " is not uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}