IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JSF Java Discussion :

t:inputFileUpload et aperçu d'images


Sujet :

JSF Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Novembre 2003
    Messages
    142
    Détails du profil
    Informations forums :
    Inscription : Novembre 2003
    Messages : 142
    Par défaut 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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    soit tu met ton autre champ en immediate pour qu'il soit mis jour avant ton valuechangelistener (pas garanti)

    soit tu fait le boulot de renommage de ton fichier dans l'action, soit après que tout le bean soit bien mis proprement.

Discussions similaires

  1. Ouvrir une image dans l'aperçu d'image windows
    Par codial dans le forum API, COM et SDKs
    Réponses: 9
    Dernier message: 18/04/2013, 14h47
  2. Aperçu des images avec gtk_file_chooser_dialog_new
    Par Julien44 dans le forum GTK+ avec C & C++
    Réponses: 1
    Dernier message: 17/08/2007, 08h41
  3. Barre d'aperçu d'images
    Par beral dans le forum C++Builder
    Réponses: 2
    Dernier message: 03/10/2006, 18h13
  4. Réponses: 4
    Dernier message: 09/08/2006, 12h30
  5. aperçu d image avec input type file marche pas dans ffx
    Par siddh dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 09/11/2005, 09h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo