JSF Temperature calculator
I am trying to create a temperature conversion program in JSF. It has got one textbox and two radio buttons to select between CEL to FRA and FRA to CEL and a submit button. I am problem getting the value of the radio buttons. I have pasted the code as under:
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}" /&开发者_如何学编程gt;
<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;
}
}
Thanks.
I think this may help you :|
Your code (I COPIED)
<h:selectOneRadio id ="radio" value="{tempconvert.radChoice}" layout="LINE_DIRECTION">
Your code (Editted)
<h:selectOneRadio id ="radio" value="#{tempconvert.radChoice}" layout="LINE_DIRECTION">
P/s: It's display: "Radio two selected" when i choose the 2nd button.
精彩评论