我的JSF页面不起作用

时间:2017-03-24 14:13:44

标签: java jsf primefaces named

我的JSF页面有问题。它应该转换温度(华氏温度,摄氏温度,开尔文温度)但是由于某种原因它不会起作用。

这是xhtml文件convtemp.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <p:layout fullPage="true">
          <p:layoutUnit position="north">
              <h1>Temperaturkonverter</h1>
            </p:layoutUnit>
          <p:layoutUnit position="center">
              <p:outputLabel value="Geben Sie eine Temperatur ein."/>
              <br/>
              <p:inputText value ="#{temperatur.temp}"/>
              <hr/>
               <p:outputLabel for="temper" value="Einheit des eingegebenen Wertes:" />
               <p:selectOneListbox id="temper" value="#{temperatur.einheit}">
        <f:selectItem itemLabel="Celsius" itemValue="c" />
        <f:selectItem itemLabel="Kelvin" itemValue="k" />
        <f:selectItem itemLabel="Fahrenheit" itemValue="f" />
    </p:selectOneListbox>
               <h:commandButton type="submit" action="#{tempControl.convertTemp(temperatur.temp)}" value="BERECHNEN"/>
   <hr/>
   <h:outputLabel for ="ergebnis" value="Ergebnisse:"/>
   <br/>
   <h:outputLabel id="ergebnis"  value="Celsius: #{temperatur.celsius}" rendered="#{temperatur.celsius !=null and temperatur.fahrenheit!= null and temperatur.kelvin != null}"/>  <br />
    <h:outputLabel id="ergebnis2"  value="Fahrenheit: #{temperatur.fahrenheit}" rendered="#{temperatur.celsius !=null and temperatur.fahrenheit!= null and temperatur.kelvin != null}"/>  <br />
     <h:outputLabel id="ergebnis3"  value="Kelvin: #{temperatur.kelvin}" rendered="#{temperatur.celsius !=null and temperatur.fahrenheit!= null and temperatur.kelvin != null}"/>  

          </p:layoutUnit>
    </p:layout>
</h:body>

这是模型Temperatur.java

package model;

import java.util.HashMap;
import java.util.Map;
import javax.inject.Named;

/**
 *
 * @author anhev
*/
@Named ("temperatur")
public class Temperatur {

private double celsius;
private double kelvin;
private double fahrenheit;
private String einheit;


private double temp;


public Temperatur() {

}


public double getCelsius() {
    return celsius;
}

public void setCelsius(double celsius) {
    this.celsius = celsius;
}

public double getKelvin() {
    return kelvin;
}

public void setKelvin(double kelvin) {
    this.kelvin = kelvin;
}

public double getFahrenheit() {
    return fahrenheit;
}

public void setFahrenheit(double fahrenheit) {
    this.fahrenheit = fahrenheit;
}



public String getEinheit() {
    return einheit;
}

public void setEinheit(String einheit) {
    this.einheit = einheit;
}

public double getTemp() {
    return temp;
}

public void setTemp(double temp) {
    this.temp = temp;
}
}

最后是控制器TempControl.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;
import model.Temperatur;

    /**
     *
     * @author anhev
     */
    @Named ("tempControl")
public class TempControl {

private Temperatur temperatur;
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
public void convertTemp(double temperaturIn){

    switch(temperatur.getEinheit()){
        case "c":
            temperatur.setCelsius(temperaturIn);                
            temperatur.setFahrenheit(temperaturIn * 1.8 + 32);
            temperatur.setKelvin(temperaturIn + 273.15);
    {
        try {
            response.sendRedirect("convtemp.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(TempControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
            break;

        case "k":
            temperatur.setKelvin(temperaturIn);
            temperatur.setCelsius(temperaturIn - 273.15);
            temperatur.setFahrenheit(temperaturIn * 1.8 - 459.67);
    {
        try {
            response.sendRedirect("convtemp.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(TempControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
            break;

        case "f":
            temperatur.setFahrenheit(temperaturIn);
            temperatur.setCelsius((temperaturIn - 32) / 1.8);
            temperatur.setKelvin((temperaturIn + 459.67) / 1.8);
    {
        try {
            response.sendRedirect("convtemp.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(TempControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
            break;


        default:
            System.out.print("Error");
            break;}
    }
}

1 个答案:

答案 0 :(得分:1)

您必须将内容放在表单中:

<h:body>
    <h:form>
         ... your code
    </h:form>
</h:body>

希望这项工作与您合作。