JSF 温度计算器
我正在尝试在 JSF 中创建一个温度转换程序。它有一个文本框和两个单选按钮,用于在 CEL 到 FRA 和 FRA 到 CEL 之间进行选择,以及一个提交按钮。我无法获取单选按钮的值。 已将代码粘贴如下:
Index.htmlTemperatureConvertBean
<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>
我
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;
}
}
谢谢。
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}" />
<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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想这可能对你有帮助:|
您的代码(我复制了)
您的代码(已编辑)
P/s:当我选择第二个按钮时,它显示:“无线电两个已选择”。
I think this may help you :|
Your code (I COPIED)
Your code (Editted)
P/s: It's display: "Radio two selected" when i choose the 2nd button.