Java 我的JSF页面赢了';行不通
我的JSF页面有问题。它应该能转换温度(华氏度、摄氏度、开尔文),但由于某些原因它不能工作 以下是xhtml文件convtemp.xhtml:Java 我的JSF页面赢了';行不通,java,jsf,primefaces,named,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">
<?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>
最后是控制器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;}
}
}
您必须将内容放入表单中:
<h:body>
<h:form>
... your code
</h:form>
</h:body>
... 你的代码
希望这能对您起作用。“行不通”是对问题的一种极其懒惰和无用的描述。请描述问题。。如果出现错误,则将堆栈跟踪添加到问题中。
<h:body>
<h:form>
... your code
</h:form>
</h:body>