Java JSF温度计算器
我正在尝试用JSF创建一个温度转换程序。它有一个文本框和两个单选按钮,可在CEL至FRA和FRA至CEL之间进行选择,还有一个提交按钮。我在获取单选按钮的值时遇到问题。我已将代码粘贴如下: Index.htmlJava JSF温度计算器,java,jsf,temperature,Java,Jsf,Temperature,我正在尝试用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">
<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>
谢谢。我想这可能会帮助您:| 您的代码(我复制了)
您的代码(已编辑)
p/s:当我选择第二个按钮时,屏幕显示:“Radio two selected”(无线电二选)。请澄清您的问题。很难理解你有什么问题。我看不出你的代码有任何错误。我认为你的bean是
@RequestScoped
,这可能是一个原因。让它@SessionScoped
试试看!祝你好运!即使它有效(我不这么认为),它也不是一个解决方案,而是一个变通办法。你基本上滥用了这里的会话范围。我知道这一点。但根据这段代码,这应该可以工作。我将其更改为RequestScope,但它仍然不工作。只有一个“#”字符。。。下次发布问题前请检查语法
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;
}
}
<h:selectOneRadio id ="radio" value="{tempconvert.radChoice}" layout="LINE_DIRECTION">
<h:selectOneRadio id ="radio" value="#{tempconvert.radChoice}" layout="LINE_DIRECTION">