您還可以使用javascript。添加表單提交或按鈕的偵聽器,在提交表單之前,使用HTML字段新值更新隱藏字段(<h:input type="hidden"/>
)值。並將該隱藏字段的值綁定到後面的bean屬性。
但我不建議使用這種方法,因爲如果您編寫JSF,您的代碼庫將會一團糟,您必須充分使用JSF功能。
更難但更標準的方法是創建一個自定義JSF組件。
要做到這一點,你必須在JSF擴展UIComponentBase
類,然後註冊您的渲染器作爲一個新的組件是這樣的:
<faces-config xmlns="http://java.sun.com/JSF/Configuration">
<component>
<component-type>tss.hello.JsfHello</component-type>
<component-class>tss.hello.HelloUIComp</component-class>
</component>
...
</faces-config>
然後,你需要這個組件添加在這樣的標籤庫(TLD文件WEB-INF文件夾):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>0.01</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<uri>http://theserverside.com/simplefacescomponents</uri>
<description>This tag library contains simple JSF Component examples.</description>
<tag>
<name>jsfhello</name>
<tag-class>tss.hello.FacesHelloTag</tag-class>
<attribute>
<name>binding</name>
<description>A value binding that points to a bean property</description>
</attribute>
<attribute>
<name>id</name>
<description>The client id of this component</description>
</attribute>
<attribute>
<name>rendered</name>
<description>Is this component rendered?</description>
</attribute>
<attribute>
<name>hellomsg</name>
<description>a custom message for the Component</description>
</attribute>
</tag>
</taglib>
然後,您可以使用您的標籤是這樣的:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://theserverside.com/customfacescomponents" prefix="cf"%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
</head>
<body>
<h:form>
<p>A simple HelloWorld Custom Component:</p>
<p>The HelloWorld UI Component:</p>
<cf:jsfhello hellomsg="Hello world! This is output from a custom JSF Component!" />
</h:form>
</body>
</html>
</f:view>
請參閱this link完成自定義組件教程
*「但是,當我使用h:inputText時,它看起來不像我使用Html輸入時」*然後您以錯誤的方式使用它。您最好退後一步,編輯並重新構建問題,詢問如何在顯示您的嘗試並告知預期的HTML輸出時正確使用它。 JSF的好處之一是你不需要再手動地調用請求參數(以及對這些參數的轉換/驗證)。 – BalusC