2016-04-24 54 views
0

我工作在一個jsf 2.2應用程序,我想使用Bootstrap。
因此,我下載了Bootstrap模板並嘗試調整它。
但是,當我使用h:inputText時,它看起來不像我使用Html輸入那麼好
這就是爲什麼我使用Html輸入,但在託管bean中,我必須使用此代碼才能獲得輸入值。使用html輸入jsf形式

HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 
    nom=req.getParameter("name"); 

我不確定它是否是最佳選擇,因爲我沒有真正使用jsf的好處。

+2

*「但是,當我使用h:inputText時,它看起來不像我使用Html輸入時」*然後您以錯誤的方式使用它。您最好退後一步,編輯並重新構建問題,詢問如何在顯示您的嘗試並告知預期的HTML輸出時正確使用它。 JSF的好處之一是你不需要再手動地調用請求參數(以及對這些參數的轉換/驗證)。 – BalusC

回答

-1

您還可以使用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完成自定義組件教程

+0

OP使用JSF 2.x而非JSF 1.x.這個答案假定JSF 1.x.此外,如果設置適當的引導程序樣式,自定義組件是絕對矯枉過正的,只需設置一個屬性權限即可,這在本答案中沒有涉及。 – BalusC

+0

是的,這是正確的,我認爲他仍然想使用他的自定義標籤。謝謝。 –