45

Shouldn't this line of code render a inputtext field with the placeholder text "fill me" when using html5?

<h:inputText placeholder="fill me" />

I do not see any placeholder text. I thought everything that was not JSF was passed to the browser for rendering?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • You have to use one of the attributes supported by that tag. Or you can make your own custom component (or composite component) to support that attribute. You can see the list of valid attributes here: http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/inputText.html – Bhesh Gurung Dec 13 '11 at 18:41
  • @gurung: a composite is not possible if the standard components/renderers already don't support it. See also http://stackoverflow.com/questions/6822000/when-to-use-uiinclude-tag-files-composite-components-and-or-custom-componen – BalusC Dec 13 '11 at 18:48
  • @BalusC: Oh ya, I didn't even think about it that it's going to be the same component that will be using even in the composite comp. Thanks for the heads up again. – Bhesh Gurung Dec 13 '11 at 18:52

9 Answers9

72

I thought everything that was not JSF was passed to the browswer for rendering?

This assumption is thus wrong. Unspecified component attributes are ignored by the JSF renderers.

You have basically the following options to get it to work:

  1. If you're already on JSF 2.2 or newer, set it as a passthrough attribute.

     <... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
    
     <h:inputText a:placeholder="fill me" />
    

    Note that I use a XML namespace prefix of a ("attribute") instead of p as shown in the tutorial, as it would otherwise clash with default XML namespace prefix p of PrimeFaces.

  2. Implement a custom renderer for <h:inputText> wherein you explicitly check and write the attribute.

  3. Implement a custom component which uses the aforementioned custom renderer.

  4. Implement a JS based solution wherein you grab the element from DOM and explicitly set the attribute.

  5. Look for a component library which supports this out the box. PrimeFaces for example has a <p:watermark> for this purpose with nice JS based graceful degradation for browsers which does not support the placeholder attribute on inputs.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
26

You can achieve it either with placeholder attribute or with p:watermark if using Primefaces and JSF 2.0+ or, when JSF 2.2 available, you can use pt:placeholder attribute.

Primefaces

<p:inputText id="search_input_id" value="#{watermarkBean.keyword}" 
    required="true" label="Keyword" placeholder="fill me" />  

Legacy browser support (Adds JS solution):

<p:inputText id="search_input_id" value="#{watermarkBean.keyword}" 
    required="true" label="Keyword" />  
<p:watermark for="search_input_id" value="fill me" />

JSF 2.2 (without PF)

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
    <h:head>
    </h:head>
    <h:body>
        <h:inputText value="#{bean.value}" pt:placeholder="fill me"/>
    </h:body>
</html>

Which basically generates an HTML 5

<input placeholder="fill me" />

Check out this answer.

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
9

With JSF 2.2 you can passthrough unspecified attributes like this:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
>

    <h:inputText p:placeholder="fill me"></h:inputText>
jmoreira
  • 1,556
  • 2
  • 16
  • 26
8

In case you are using RichFaces, starting in version 4.3, you can use the tag "rich:placeholder" for this purpose as shown here. Basically:

<h:inputText id="myInput">
    <rich:placeholder value="My placeholder text"></rich:placeholder>
</h:inputText>
3

Try this

<h:inputText id="name" value="#{login.userId}" class="aux1" />
<h:inputSecret id="password" value="#{login.password}" redisplay="true" class="aux2" autocomplete="off" />

<script>
  $('.aux1').attr('placeholder', 'Introducir Usuario');
  $('.aux2').attr('placeholder', 'Introducir Contraseña');
</script>

With jQuery, this works right for me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

It's very easy and browser independent code as BaluSc told, In primefaces, use p:watermark to get the required functionality. Official Demo is HERE

Mr.Chowdary
  • 3,389
  • 9
  • 42
  • 66
0

Use primeface 4.0. Versions below this version do not support the placeholder attribute.

  1. use name space xmlns:pt="http://java.sun.com/jsf/passthrough".

  2. p:inputTextarea id="textAreaValue" pt:placeholder="your text"

    don't insert a new line in inputTextArea.

Community
  • 1
  • 1
0

The simplest way to render an input field with a placeholder text is to use the elementary input tag

Example:

<input type="text" placeholder="Fill me" value="#{EL}"/>

Note: you dont have to include any namespaces

Ayoub Falah
  • 484
  • 3
  • 19
-1

<h:head>
</h:head>
<h:body>
    <h:inputText value="#{bean.value}" placeholder="fill me"/>
</h:body>

This works right for me, try it!