2011-10-16 12 views
-1

JSFで温度変換プログラムを作成しようとしています。 1つのテキストボックスと2つのラジオボタンがあり、CELからFRA、FRAからCEL、および送信ボタンを選択できます。私はラジオボタンの価値を得ることに問題があります。私は下のようなコードを貼り付けていますJSF温度計算機

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

問題を明確にしてください。どのような問題があるのか​​を理解するのは難しいです。 – maks

+0

あなたのコードに間違いはありません。私はあなたのbeanが '@ RequestScoped'だと思っています。これは理由かもしれません。' SessionScoped'を試してみてください! 運が良かった! –

+0

それが働いても(私はそうは思わない)、それは解決策ではありませんでしたが、回避策でした。あなたは基本的にここでスコープされたセッションを乱用しています。 – BalusC

答えて

1

これはあなたを助けるかもしれないと思います:

あなたのコード(私はをコピーした)

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

あなたのコード(Editted)

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

P/S: "ラジオ選択された2つの" 私は第二ボタンを選択すると:それはディスプレイです。

+0

優れています。今働いている。ありがとう@Bachboss – user997611

+0

ただ1つの "#"文字...次回質問を投稿する前にあなたの構文をチェックしてください – BachT