我想在JSF / Primefaces inputText 组件中显示默认的信息性文本。
我知道在ManagedBean中将默认值设置为BeanProperty会起作用,但我不希望这样。
如果可能,请建议任何JQuery调整。
如果此问题是重复,请将我重定向到问题。
答案 0 :(得分:14)
Primefaces 在其最新版本中提供placeholder
属性,您可以将其用于p:inputText
。此外,还有一个p:watermark
组件,可为旧版浏览器兼容性创建基于JS的解决方案。因此,您无需在支持bean中设置默认值。只需使用以下解决方案之一:
<h:outputLabel value="Search: "/>
<p:inputText id="search_input_id" value="#{watermarkBean.keyword}"
required="true" label="Keyword" placeholder="search" />
对于旧版浏览器:
<h:outputLabel value="Search: "/>
<p:inputText id="search_input_id" value="#{watermarkBean.keyword}"
required="true" label="Keyword" />
<p:watermark for="search_input_id" value="search" />
此外,如果使用 JSF 2.2 ,您可以使用其passthrough属性。在页面中添加xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
命名空间,您可以通过以下方式实现JSF h:inputText
和Primefaces p:inputText
:
<h:inputText value="#{watermarkBean.keyword}"
pt:placeholder="search" />
使用TagHandler将其包装到标记中:
<h:inputText value="#{watermarkBean.keyword}">
<f:passThroughAttribute name="placeholder"
value="search"/>
</h:inputText>
使用placeholder
属性创建HTML 5 based输入:
<input placeholder="search">