2012-04-12 41 views
4

所以我的團隊有一個jsf,我們使用EL語句讀取輸入框的值,工作正常,但默認情況下,我們已將該值設置爲應填寫到現場。我怎樣才能設置輸入的默認值,同時仍然有EL語句的工作。 例子:在使用EL語句讀取值時設置輸入字段的默認值

<h:inputText value="#{registrationBean.firstName}" styleClass="formEle"/> 

我試圖

<h:inputText value="#{registrationBean.firstName}First Name" styleClass="formEle"/> 

但這打破,當我們提交連接。 任何想法?

回答

10

您正在尋找稱爲「水印」或「佔位符」的東西。在低的水平,這幾天這通常是由HTML5 placeholder屬性來完成:

<input type="text" placeholder="First Name" /> 

這隻有在瀏覽器支持HTML5的工作過程,然後只與JSF組件支持新的HTML5特性。標準JSF <h:inputText>本身不支持(尚)。你需要尋找支持這個的第三方組件庫。其中之一是與PrimeFaces其<p:watermark>

<h:inputText id="firstName" value="#{registrationBean.firstName}" /> 
<p:watermark for="firstName" value="First Name" /> 

如果使用PrimeFaces是不是出於某種原因的選項,你需要自己重新塑造它在一個自定義組件和/或JS/jQuery的自定義一塊味道,就像<p:watermark>正在做的一樣。

-1

如果您決定使用您不使用水印需要PrimeFaces,見下面的例子:加載插入一個佔位符attibute爲HTML5之後

<h:form> 
    <p:panel id="panel" header="Client Details" style="margin-bottom:10px;"> 
    <p:messages id="messages" /> 
    <h:panelGrid columns="2"> 

     <h:outputLabel for="firstName" value="First Name" /> 
     <p:inputText id="firstName" value="#{clientlistpagebean.selectedFieldClient.firstName}" required="true" /> 

     <p:outputLabel for="lastName" value="Last Name" /> 
     <p:inputText id="lastName" value="#{clientlistpagebean.selectedFieldClient.lastName}" required="true" /> 

    </h:panelGrid> 
    </p:panel> 

    <p:commandButton value="Submit" update="panel" style="margin-right:20px;" /> 
</h:form> 
1

由JSF呈現可以由JavaScript修改的HTML標籤:

<script> 
    window.onload = function() { 
    document.getElementById('email').setAttribute("placeholder", "Email"); 
    document.getElementById('pwd').setAttribute("placeholder", "Password"); 
    } 
</script> 
2

你可以在PostConstrut階段支撐bean定義<h:inputText>默認值。

@ManagedBean(name="registrationBean") 
public class RegistrationBean{ 

    private String firstName; 

    @PostConstruct 
    public void init(){ 
     firstName = "your default value"; 
    } 

} 

經過h:inputText顯示的頁面創造的價值會隨着你的支持bean定義。 這是一種無需任何第三方庫的工作,也許它會幫助某人。