我正在尝试在JSF中创建温度转换程序。它有一个文本框和两个单选按钮,可以在CEL到FRA和FRA到CEL之间进行选择,还有一个提交按钮。我很难获得单选按钮的值。我已将代码粘贴在:
下面的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;
}
}
感谢。
答案 0 :(得分:1)
我认为这可能会对您有所帮助:|
您的代码( I COPIED )
<h:selectOneRadio id ="radio" value="{tempconvert.radChoice}" layout="LINE_DIRECTION">
您的代码(编辑)
<h:selectOneRadio id ="radio" value="#{tempconvert.radChoice}" layout="LINE_DIRECTION">
P / s:当我选择第二个按钮时显示:“Radio two selected”。