【问题标题】:Placeholder is not working in PrimeFaces p:inputText占位符在 PrimeFaces p:inputText 中不起作用
【发布时间】:2016-08-26 03:34:21
【问题描述】:

我正在尝试使用 PrimeFaces inputText 文本框来获取用户输入。即使我添加了占位符属性,文本也没有出现在文本框中。我需要为此包含任何库吗?下面是我的jsf页面。

 <ui:composition template="/template/master-layout.xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:p="http://primefaces.org/ui">
     <ui:define name="head">  test header </ui:define>

    <ui:define name="meta">
        <f:metadata>
            <f:event type="preRenderView" listener="#{HomeBean.initPage}"></f:event>
        </f:metadata>
    </ui:define>
      <ui:define name="mainContainer">
        <div class="row">
          <div class="col-sm-12">
            <p:inputText placeholder="Your ID" styleClass="form-control service-input-border" maxlength="20" id="Id"
               value="#{homeBean.Id}"></p:inputText>
         </div>
        </div>
    </ui:define>
</ui:composition>

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    Apostolos 的回答很好,但我更喜欢使用直通属性。您可以使用它们如果您使用的是 JSF >= JSF 2.2

    只需使用新的命名空间 http://xmlns.jcp.org/jsf/passthrough 并使用您喜欢的任何 HTML 属性(例如,任何未添加到输入/组件中的属性)。在您的情况下,它将是 placeholder

    <ui:composition template="/template/master-layout.xhtml"
            xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
            xmlns:f="http://xmlns.jcp.org/jsf/core"
            xmlns:h="http://xmlns.jcp.org/jsf/html"
            xmlns:p="http://primefaces.org/ui"
            xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">    
    
    <!-- ... -->
    
    <p:inputText pt:placeholder="Your ID" styleClass="form-control service-input-border"
        maxlength="20" id="Id" value="#{homeBean.Id}" />
    

    您可以阅读有关直通属性herehere 的更多信息。

    【讨论】:

    • 有趣。我不知道。我使用的是经典的&lt;f:passThroughAttribute name="XXX" value="XXX" /&gt; 标签。点赞!
    • @Apostolos 是的,最后是一样的,但是f:passThroughAttribute 更长,更冗长;-)
    【解决方案2】:

    你应该使用watermark标签来显示占位符。

    <p:inputText styleClass="form-control service-input-border" maxlength="20" id="Id" value="#{homeBean.Id}"></p:inputText>
    <p:watermark for="Id" value="Your ID" id="watermark" />
    

    也可以查看showcase

    【讨论】:

    • 还是一样。我试过这个。这就是为什么我想知道我错过的任何库。
    • 我在将其发布为答案之前尝试了它并且它正在工作。没有额外的库。小心身份证。 for 指的是正确的id
    猜你喜欢
    • 2018-02-19
    • 2013-10-19
    • 2017-04-23
    • 2013-07-15
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多