三、
JSP如何响应JSF的请求
从上面的例子我们已经知道如何在JSP中使用JSF了,在这一部分让我们来看看在JSF是如何处理请求的。
首先让我们来看一个例子,这个例子是将华氏度转换为摄氏度。当用户点击提交按钮时程序将进行转换。
<%@ taglib uri=http://java.sun.com/jsf/htmlprefix="h" %>
<%@ taglib uri=http://java.sun.com/jsf/core prefix="f" %>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=GB2312">
<title>温度转换程序</title>
</head>
<body>
<f:view>
<h:form>
<div>
<h:outputText id="fahrenheitLabel" value="请输入华氏温度:"/>
<span>
<h:inputText id="temperature" value="#{tc.fahrenheitTemp}">
<f:validateDoublerange minimum="-100.0" maximum="100.0"/>
<f:valuechangeListener type="tempconv.page.TCChangedListener"/>
</h:inputText>
</span>
</div>
<div>
<h:outputText id="celsiusLabel" value="摄氏温度:"/>
<span>
<h:outputText id="celsiusValue" value="#{tc.celsiusTemp}">
<f:convertNumber maxFractionDigits="3" type="number"/>
</h:outputText>
</span>
</div>
<div>
<h:commandButton value="转换" action="#{tc.convert}">
</h:commandButton>
</div>
</h:form>
</f:view>
</body>
</html>
在程序的前两行是导入JSF核心库和HTML库,这个在前面已经讨论过,在这里不再详述。