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 :

selectOneMenu et action sur champ input


Sujet :

JSF Java

  1. #1
    Membre habitué
    Inscrit en
    Avril 2010
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 9
    Par défaut selectOneMenu et action sur champ input
    Je voudrais créer une liste de dévise (euro, yen, dollar) et y accéder à partir d'une liste déroulante à choix unique pour ensuite formater un montant. Si je chosi euro ou dollar alors je veux que le montant (input)soit écris avec 2 décimales à la fin, sinon en cas de choix du yen, je voudrais qu'il y ait 3 décimales. Je n'arrive pas à créer une classe qui me permettrait de formater le montant en fonction de la devise choisie. Quelqu'un peut il m'aider? Merci pour vos reponses

  2. #2
    Rédacteur

    Profil pro
    Inscrit en
    Juin 2003
    Messages
    4 184
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 4 184
    Par défaut
    tu peux utiliser trois validator et utiliser l'attribut rendered pour utiliser un des trois, ou faire un validator personnalisé, qui prend en compte la valeur de la devise choisit.

  3. #3
    Membre habitué
    Inscrit en
    Avril 2010
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 9
    Par défaut selectOneMenu et action sur champ input
    ci joint la partie de ma page jsp qui me permet d'acceder à la liste des dévises:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     <h:inputText
    								value="#{offreEligibleBean.montant}" required="true"
    								onchange="submit"> 
    								<f:convertNumber "/>
    								</h:inputText>
    								<cgit2:selectOneMenu
    								id="list_deroulante_devise" onchange="this.form.submit()"
    								styleClass="list_roulante_1" value="#{currency.defaultName}"
    								valueChangeListener="#{currencyChg.processValueChange}">
    								<f:selectItems id="devise"
    									value="#{offreEligibleBean.currencyNameList}"/>
    							</cgit2:selectOneMenu> <br>
    le formatage est géré par cette classe
    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
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
     public static String doAmountConvertion(final String montant,
    			final String currencyName) {
    		CurrencyBean currencyDescriptor = getCurrencyDescriptor(currencyName);
     
    		String convertedCurrency = createDisplayableCurrency(montant,
    				currencyDescriptor);
     
    		return convertedCurrency;
    	}
     
    	private static final String FRENCH_DECIMAL_SEPARATOR = ",";
     
    	private static String createDisplayableCurrency(String amount,
    			CurrencyBean currencyDescriptor) {
     
    		String pattern = buildPattern(currencyDescriptor, amount);
     
    		Double convertibleNumber = new Double(buildConvertibleNumber(amount));
     
    		Locale local = new Locale("fr","FR");
     
    		NumberFormat formater = NumberFormat.getNumberInstance(local);
     
    		DecimalFormat decimalFormater = (DecimalFormat)formater;
     
    		decimalFormater.applyPattern(pattern);
    		String convertedCurrency = decimalFormater.format(convertibleNumber);
     
    		convertedCurrency = convertedCurrency.replace(FRENCH_DECIMAL_SEPARATOR, DECIMAL_SEPARATOR);
    		fwsLogger.debug("buildPattern : convertedCurrency=[" + convertedCurrency + "]");
     
    		StringBuffer outputBuffer = replaceSpace160BySpace32(convertedCurrency);
     
    		return outputBuffer.toString();
    	}
     
    	private static StringBuffer replaceSpace160BySpace32(
    			String convertedCurrency) {
    		StringBuffer outputBuffer = new StringBuffer();
    		for(int index=0; index<convertedCurrency.length(); index++) {
    			char actualChar = convertedCurrency.charAt(index);
    			int a = actualChar;
    			if(a == 160) {
    				outputBuffer.append(" ");
    			} else {
    				outputBuffer.append(actualChar);
    			}
    		}
    		return outputBuffer;
    	}
     
    	private static String buildConvertibleNumber(String amount) {
    		String convertibleNumber = "";
    		if (amount.contains(DECIMAL_SEPARATOR)) {
    			convertibleNumber = amount;
    		} else {
    			convertibleNumber = amount + DECIMAL_SEPARATOR + FILLING_ELEMENT;
    		}
    		return convertibleNumber;
    	}
     
    	private static final int THOUSAND_SIZE = 3;
     
    	private static final String FILLING_ELEMENT = new String("0");
     
    	private static final String INTEGER_PATTERN_BLOCS_SEPARATOR = new String(",");
     
    	private static String buildPattern(CurrencyBean currencyDescriptor,
    			String amount) {
     
    		StringBuffer buffer = new StringBuffer();
     
    		buffer.append(buildPatternForIntegerPart(amount));
     
    		buffer.append(DECIMAL_SEPARATOR);
     
    		buildPatternForDecimalPart(currencyDescriptor, buffer);
     
    		fwsLogger.debug("buildPattern : pattern=[" + buffer.toString() + "]");
     
    		return buffer.toString();
    	}
     
    	private static void buildPatternForDecimalPart(
    			CurrencyBean currencyDescriptor, StringBuffer buffer) {
    		final int decimalPartSize = currencyDescriptor.getDecimalPartSize();
    		for (int index = 0; index < decimalPartSize; index++) {
    			buffer.append(FILLING_ELEMENT);
    		}
    	}
    	private static String buildPatternForIntegerPart(String amount) {
    		StringBuffer inputBuffer = new StringBuffer();
     
    		String currentIntergerPart = getIntergerPartFromCurrencyAmount(amount);
     
    		buildReversedPattern(inputBuffer, currentIntergerPart);
     
    		StringBuffer outputBuffer = reverseBufferContent(inputBuffer);
     
    		return outputBuffer.toString();
    	}
     
    	private static void buildReversedPattern(StringBuffer inputBuffer,
    			String currentIntergerPart) {
    		final int patternBlocsNumberForIntergerPart = currentIntergerPart
    				.length()
    				/ THOUSAND_SIZE + 1;
     
    		// Exemple pattern : 000 000.00
    		for (int index = 0; index < patternBlocsNumberForIntergerPart; index++) {
    			if (index > 0) {
    				inputBuffer.append(INTEGER_PATTERN_BLOCS_SEPARATOR);
    			}
     
    			int addedElementNumber = buildAddedElementNumber(currentIntergerPart, index);
     
    			for (int addedElementCount = 0; addedElementCount < addedElementNumber; addedElementCount++) {
    				inputBuffer.append(FILLING_ELEMENT);
    			}
    		}
    	}
     
    	private static StringBuffer reverseBufferContent(StringBuffer inputBuffer) {
    		StringBuffer outputBuffer = new StringBuffer();
    		final int patternSize = inputBuffer.length();
    		int atPosition = patternSize - 1;
    		for(int index = 0; index < patternSize; index++) {
    			outputBuffer.append(inputBuffer.charAt(atPosition));
    			atPosition--;
    		}
    		return outputBuffer;
    	}
     
    	private static int buildAddedElementNumber(
    			final String currentIntergerPart, final int currentBlocIndex) {
     
    		int blocsnumber = currentIntergerPart.length() / THOUSAND_SIZE;
     
    		int addedElementNumber = 0;
     
    		if (currentBlocIndex < blocsnumber) {
    			addedElementNumber = THOUSAND_SIZE;
    		} else {
    			addedElementNumber = currentIntergerPart.length() - (blocsnumber * THOUSAND_SIZE); //blocsnumber;
    		}
     
    		return addedElementNumber;
    	}
     
    	private static final String DECIMAL_SEPARATOR = new String(".");
     
    	private static String getIntergerPartFromCurrencyAmount(String montant) {
     
    		String intergerPartFromCurrencyAmount = "";
     
    		final int separatorPosition = montant.indexOf(DECIMAL_SEPARATOR);
     
    		try {
    			intergerPartFromCurrencyAmount = montant.substring(0,
    					separatorPosition);
    		} catch (StringIndexOutOfBoundsException e) {
    			intergerPartFromCurrencyAmount = montant;
    		}
     
    		return intergerPartFromCurrencyAmount;
    	}
     
    	private static CurrencyBean getCurrencyDescriptor(final String currencyName) {
    		fwsLogger.debug("getCurrencyDescriptor : currencyName=[" + currencyName
    				+ "]");
    		return CurrencyFactory.getUniqueInstance().buildCurrency(currencyName);
    	}
    et la liste récupérée ici
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     public List<SelectItem> getCurrencyNameList() {
    		if (null == CurrencyNameList) {
    			CurrencyNameList = new ArrayList<SelectItem>();
     
    			SelectItem dollarItem = new SelectItem(CurrencyBean.getDollarName());
    			CurrencyNameList.add(dollarItem);
     
    			SelectItem euroItem = new SelectItem(CurrencyBean.getEuroName());
    			CurrencyNameList.add(euroItem);
     
    			SelectItem yenItem = new SelectItem(CurrencyBean.getYenName());
    			CurrencyNameList.add(yenItem);
    		}
    		return CurrencyNameList;
    .
    Quelqu'un pourra t'il donc me dire comment faire pour afficher le montant formatté une fois la la dévise choisie?
    Merci

  4. #4
    Rédacteur

    Profil pro
    Inscrit en
    Juin 2003
    Messages
    4 184
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 4 184
    Par défaut
    regarde les questions qui parlent des converters dans la

  5. #5
    Membre habitué
    Inscrit en
    Avril 2010
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 9
    Par défaut
    Il n'y a pas de solution pour mon problème dans la faq.

  6. #6
    Rédacteur

    Profil pro
    Inscrit en
    Juin 2003
    Messages
    4 184
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 4 184
    Par défaut
    Citation Envoyé par LGB9498 Voir le message
    Il n'y a pas de solution pour mon problème dans la faq.
    y'a aussi des tutoriels qui expliquent le fonctionnement des converters/validators. un autre

  7. #7
    Membre habitué
    Inscrit en
    Avril 2010
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Avril 2010
    Messages : 9
    Par défaut
    Les tuto n'ont plu ne reponde pas à ma question. Est ce à dire que personne n'a jamais eu à tratiter ce cas ici?

Discussions similaires

  1. Simuler clic sur champs input text
    Par mikl86 dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 02/11/2012, 14h25
  2. Problème de style CSS sur champ input text
    Par elekaj34 dans le forum Mise en page CSS
    Réponses: 3
    Dernier message: 14/12/2011, 10h53
  3. Curseur inactif sur champ input
    Par Kara dans le forum Balisage (X)HTML et validation W3C
    Réponses: 1
    Dernier message: 28/01/2010, 15h51
  4. Ombrage sur champ Input
    Par stanley dans le forum Mise en page CSS
    Réponses: 5
    Dernier message: 11/01/2008, 14h37
  5. saisie semi-automatique sur champs input sans submit du formulaire
    Par j0hnmerrick dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 24/10/2007, 17h54

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