2012-04-12 40 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定义。 这是一种无需任何第三方库的工作,也许它会帮助某人。