t:inputFileUpload et aperçu d'images
Bonjour,
J'essaye d'utiliser le composant de tomahawk pour uploader des images et afficher leur aperçu.
Je souhaite également que le nom du fichier uploadé soit généré à l'aide du contenant d'un champ du formulaire et c'est là tout mon problème.
En effet, j'utilise un valueChangeListener afin que l'image soit uploadé une fois celle-ci choisie mais le souci est que j'ai besoin du texte saisi dans le inputText du formulaire pour générer le nom de mon fichier, hors la propriété mappée avec l'inputText est nulle.
Comme le valueChangeListener est invoquée avant la phase "UpdateModelValues" qui met à jour les propriétés du backing bean, j'ai décalé l'appel du valueChangeListener mais ceci n'y change rien :(
Voici un extrait de mon code :
VUE
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <tr height="20">
<td class="line3 x-form-item"><h:outputText id="labelPrenomRepresentant" value="#{ogtf_label['ogtf.label.prenomRepresentant']}"/></td>
<td class="line0 x-form-item" colspan="2">
<h:inputText class="champ-saisie" maxlength="25" id="prenomRepresentant" value="#{societeEditViewController.selectedSociete.prenomRepresentant}" />
</td>
</tr>
<tr height="20">
<td class="line3 x-form-item"><h:outputText id="labelSignatureRepresentant" value="#{ogtf_label['ogtf.label.signatureRepresentant']}"/></td>
<td class="line0 x-form-item" colspan="1">
<t:inputFileUpload id="import_file" accept="image/*" styleClass="champ-saisie" storage="file" onchange="submit();">
<f:valueChangeListener type="com.rte.ogtf.presentation.donnees.societes.SocieteEditViewController"/>
</t:inputFileUpload>
</td>
</tr>
<tr height="20">
<td class="line3 x-form-item"></td>
<td class="line0 x-form-item" colspan="2">
<h:graphicImage id="signature" url="#{societeEditViewController.selectedSociete.signatureRepresentant}"/>
</td>
</tr> |
CONTROLEUR
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| /**
* Société sélectionnée
*/
private Societe selectedSociete;
/**
* Holder de la recherche en session
*/
private RechercheListeHolder rechercheListeHolder;
@Override
public void init() {
selectedSociete = new Societe();
selectedSociete.setId(Long.valueOf(rechercheListeHolder.getSelectedId()));
}
@Override
public void prerender() {
if (!this.isPostBack()) {
// mode édition
if (selectedSociete.getId() != null) {
try {
selectedSociete = societesService.findSocieteByPk(selectedSociete.getId());
} catch (Exception e) {
MessageSupport.addMessage(FacesMessage.SEVERITY_ERROR, OgtfConstants.RESOURCE_OGTF_MESSAGE, "ogtf.erreur.baseDonnees");
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}
}
public String modifier() {
return OgtfConstants.OUTCOM_SUCCESS;
}
/**
* Retour à la page de liste
* @return
*/
public String retour() {
rechercheListeHolder.clearMessages();
return OgtfConstants.OUTCOM_SUCCESS;
}
public SocietesService getSocietesService() {
return societesService;
}
public void setSocietesService(SocietesService societesService) {
this.societesService = societesService;
}
public Societe getSelectedSociete() {
return selectedSociete;
}
public void setSelectedSociete(Societe selectedSociete) {
this.selectedSociete = selectedSociete;
}
public RechercheListeHolder getRechercheListeHolder() {
return rechercheListeHolder;
}
public void setRechercheListeHolder(RechercheListeHolder rechercheListeHolder) {
this.rechercheListeHolder = rechercheListeHolder;
}
public ParameterService getParameterService() {
return parameterService;
}
public void setParameterService(ParameterService parameterService) {
this.parameterService = parameterService;
}
public FileService getFileService() {
return fileService;
}
public void setFileService(FileService fileService) {
this.fileService = fileService;
}
public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
PhaseId phaseId = event.getPhaseId();
if (phaseId.equals(PhaseId.ANY_PHASE)) {
event.setPhaseId(PhaseId.UPDATE_MODEL_VALUES);
event.queue();
} else if (phaseId.equals(PhaseId.UPDATE_MODEL_VALUES)) {
UploadedFile file = (UploadedFile) event.getNewValue();
if (file != null) {
if (file.getContentType().startsWith("image")) {
try {
// chargement de l'image
Image image = ImageIO.read(file.getInputStream());
// vérification des dimensions
if (image.getWidth(null) > OgtfConstants.IMAGE_WIDTH || image.getHeight(null) > OgtfConstants.IMAGE_HEIGHT) {
throw new ImageDimensionException("Dimensions non respectées [" + OgtfConstants.IMAGE_WIDTH + " x "
+ OgtfConstants.IMAGE_HEIGHT + "]");
}
// if (selectedSociete.getPrenomRepresentant() != null && selectedSociete.getPrenomRepresentant().trim().length() > 0
// && selectedSociete.getNomRepresentant() != null && selectedSociete.getNomRepresentant().trim().length() > 0) {
//
// // racine d'OGTF
// String ogtfPath = parameterService.getParameterValue(UploadConstants.UPLOAD_DOMAIN_KEY, UploadConstants.OGTF_PATH_KEY);
//
// // répertoire des données Sociétés
// String societesDir = parameterService.getParameterValue(UploadConstants.UPLOAD_DOMAIN_KEY,
// UploadConstants.OGTF_DONNEES_SOCIETES_DIR_KEY);
//
// // nom du fichier uploadé sur le serveur
// String signatureFileName = FileService.PREFIX_SIGNATURE + selectedSociete.getPrenomRepresentant() + "-"
// + selectedSociete.getNomRepresentant() + "." + file.getName();
//
// // upload de l'image
// if (ogtfPath != null && ogtfPath.trim().length() > 0 && societesDir != null && societesDir.trim().length() > 0) {
// String signatureFilePath = ogtfPath + File.pathSeparator + societesDir + File.pathSeparator + signatureFileName;
// fileService.saveFile(signatureFilePath, file.getBytes());
// }
// }
// MessageSupport.addMessage(FacesMessage.SEVERITY_ERROR, OgtfConstants.RESOURCE_OGTF_MESSAGE, "ogtf.edition.erreur.image.io");
} catch (ImageDimensionException ile) {
MessageSupport.addMessage(FacesMessage.SEVERITY_ERROR, OgtfConstants.RESOURCE_OGTF_MESSAGE,
"ogtf.edition.erreur.image.dimensions", new String[] { String.valueOf(OgtfConstants.IMAGE_WIDTH),
String.valueOf(OgtfConstants.IMAGE_HEIGHT) });
logger.error(ile.getMessage(), ile);
} catch (IOException ioe) {
MessageSupport.addMessage(FacesMessage.SEVERITY_ERROR, OgtfConstants.RESOURCE_OGTF_MESSAGE, "ogtf.edition.erreur.image.io");
logger.error(ioe.getMessage(), ioe);
}
}
}
}
} |
La référence "selectedSociete" est nulle (partie en commentaires) dans ma méthode "processValueChange" alors que j'en ai besoin pour récupérer la propriété "prenomRepresentant" qui servira à générer le nom de mon fichier.
Est-ce insurmontable techniquement ?
Je me suis plongé dans les phases de JSF, j'ai essayé diverses alternatives mais sans succès.