J'essaie de créer un programme de conversion de température en JSF. Il a une zone de texte et deux boutons radio pour sélectionner entre CEL à FRA et FRA à CEL et un bouton de soumission. Je n'arrive pas à obtenir la valeur des boutons radio. J'ai collé le code comme sous:JSF Calculatrice de température
Index.html
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Convert Temperature</title>
</h:head>
<h:body>
<h1>Convert Temperature </h1>
<f:view>
<h:form id="tempForm">
<h:outputText value="Enter Temperature:"/>
<h:inputText value="#{tempconvert.temperature}" />
<h:selectOneRadio id ="radio" value="{tempconvert.radChoice}" layout="LINE_DIRECTION">
<f:selectItem itemValue="radOne" itemLabel="CEL to FAR" />
<f:selectItem itemValue ="radTwo" itemLabel="FAR to CEL" />
</h:selectOneRadio>
<h:commandButton action="#{tempconvert.ConvertTemp}" value="Convert" />
</h:form>
<br />
<h:outputLabel value="#{tempconvert.resultlabel}" />
</f:view>
</h:body>
</html>
TemperatureConvertBean
package TemperatureConvert;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.*;
@ManagedBean(name = "tempconvert")
@RequestScoped
public class TemperatureConvertBean {
private double temperature;
private String resultlabel;
private String radChoice = "radOne";
/** Creates a new instance of TemperatureConvertBean */
public TemperatureConvertBean() {
}
/**
* @return the temperature
*/
public double getTemperature() {
return temperature;
}
/**
* @param temperature the temperature to set
*/
public void setTemperature(double temperature) {
this.temperature = temperature;
}
/**
* @return the resultlabel
*/
public String getResultlabel() {
return resultlabel;
}
/**
* @param resultlabel the resultlabel to set
*/
public void setResultlabel(String resultlabel) {
this.resultlabel = resultlabel;
}
/**
* @return the radChoice
*/
public String getRadChoice() {
return radChoice;
}
/**
* @param radChoice the radChoice to set
*/
public void setRadChoice(String radChoice) {
this.radChoice = radChoice;
}
public String ConvertTemp() {
if (this.getRadChoice().equals("radOne"))
{
this.resultlabel = "Radio one selected";
}
else
{
this.resultlabel = "Radio two selected";
}
return null;
}
}
Merci.
Clarifiez votre problème, s'il vous plaît. Il est difficile de comprendre quel problème avez-vous? – maks
Je ne vois pas de problème dans votre code. Je pense que votre bean est '@ RequestScoped', cela peut être une raison. Faites-le' @ SessionScoped' pour essayer! Bonne chance! –
Même si ça a marché (je ne le pense pas), ça n'aurait pas été une solution, mais une solution de contournement. Vous abusez essentiellement de la session délimitée ici. – BalusC