JSF 温度计算器

发布于 2024-12-09 19:49:20 字数 2867 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-12-16 19:49:20

我想这可能对你有帮助:|

您的代码(我复制了

<h:selectOneRadio id ="radio" value="{tempconvert.radChoice}"  layout="LINE_DIRECTION">

您的代码(已编辑)

<h:selectOneRadio id ="radio" value="#{tempconvert.radChoice}"  layout="LINE_DIRECTION">

P/s:当我选择第二个按钮时,它显示:“无线电两个已选择”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文