139

Eg: h:inputText will render a "input type='text'". What jsf tag can render a "div" tag?

Rajan
  • 199
  • 1
  • 5
  • 20
user101442
  • 2,517
  • 3
  • 20
  • 12

5 Answers5

247

You can create a DIV component using the <h:panelGroup/>. By default, the <h:panelGroup/> will generate a SPAN in the HTML code.

However, if you specify layout="block", then the component will be a DIV in the generated HTML code.

<h:panelGroup layout="block"/>
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • This does not work for me. Layout is listed as an invalid attribute for the panelGroup tag. What are the minumun requires for this? What level of JSf, etc? – GC_ Mar 04 '11 at 15:43
  • @Grae, Layout is a valid attribute under 1.2, can't speak for other versions however. – James McMahon Apr 01 '11 at 14:49
  • 10
    Doesn't render for me unless I add a style or style class to the panelgroup, using the mojarra-1.2_15 implementation. – James McMahon Apr 01 '11 at 14:50
  • 3
    Addendum, or set the ID attribute. If I don't do this, it just shows up in the HTML without the surrounding DIV tags. – James McMahon Apr 01 '11 at 14:56
  • 1
    shouldnt we straightaway use the `div` tag itself ? What may be the problem if any ? – Rajat Gupta Jul 06 '11 at 07:14
  • 5
    @Marcos Of course, you can use the `div` tag, but in this case, this component will not be part of the JSF component tree (i.e. not available in the Java code). In addition to that, you will not be able to refresh it directly using Ajax (except, of course, if you do the refresh directly in some of your JavaScript code). – Romain Linsolas Jul 06 '11 at 07:45
  • Thanks for clarification. Aren't even the containing jsf components inside the parent `div` added to jsf component tree ? – Rajat Gupta Jul 06 '11 at 07:49
  • @Marcos No, they will be rendered at `RENDER_REPONSE` phase but not accessible through the parent component. – Romain Linsolas Jul 06 '11 at 10:34
  • This is more of an aside or commentary (especially since this is 5 years old at this point and I have no real skill or knowledge of Primefaces, I have to hack together support): Why the heck wouldn't JSF Primfaces include div functionality natively? This hack job really is ineffective and ruins the semantic structure of HTML – lilott8 Jun 09 '14 at 21:20
  • @lilott8 why would you Primefaces to include a `div` component in its library, as you have both `
    ` (HTML tag) or this core JSF component for that?
    – Romain Linsolas Jun 10 '14 at 06:56
  • 1
    The answer is only half of the truth. See here http://docs.oracle.com/javaee/5/javaserverfaces/1.2/docs/tlddocs/h/panelGroup.html or here http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/panelGroup.html. The important piece of info is that you also need one `style` or `styleClass` attribute for a `div` to happen... which makes the story absolutely strange IMHO: a plain `` will render a ``? It doesn't make sense to me. – Kawu Aug 18 '14 at 14:23
  • Adding to @romaintaz 's comments: I find this `` useful (amongst others) because one can set the `rendered` attribute. This allows for *conditionally* displaying a component in a view, in other words *dynamically changing* it. As s/he explained, the final HTML `
    ` is only generated at render time, when the values used in the render condition are already available. When one uses HTML `
    ` directly, this is "set in stone" already by the time the view is built. And that is good enough for many cases: static parts of the view, so choose whatever is best.
    – frIT Jan 27 '16 at 11:04
  • If `` doesn't work for you, try ``. – domids Dec 11 '19 at 16:08
20

In JSF 2.2 it's possible to use passthrough elements:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:jsf="http://xmlns.jcp.org/jsf">
    ...
    <div jsf:id="id1" />
    ...
</html>

The requirement is to have at least one attribute in the element using jsf namespace.

mp31415
  • 6,531
  • 1
  • 44
  • 34
14

Apart from the <h:panelGroup> component (which comes as a bit of a surprise to me), you could use a <f:verbatim> tag with the escape parameter set to false to generate any mark-up you want. For example:

<f:verbatim escape="true">
    <div id="blah"></div>
</f:verbatim>

Bear in mind it's a little less elegant than the panelGroup solution, as you have to generate this for both the start and end tags if you want to wrap any of your JSF code with the div tag.

Alternatively, all the major UI Frameworks have a div component tag, or you could write your own.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Nick Grubb
  • 159
  • 2
3

you can use myfaces tomahawk component

http://myfaces.apache.org/tomahawk-project/tomahawk12/tagdoc/t_div.html

jack jin
  • 1,254
  • 10
  • 12
  • I even removed tomahawk from a project. Afterwards the project was much faster! I wouldn´t recommend to use tomahawk! – marcel Nov 16 '15 at 16:11
  • 1
    I make this answer in 2011. now, I am using primefaces, and feels very good of it – jack jin Jan 08 '16 at 07:49
-3

I think we can you use verbatim tag, as in this tag we use any of the HTML tags

Sanket
  • 355
  • 5
  • 14
  • Seriously? WHY? Verbatim tag is something that was relevant in the jsp era. In the facelets era (10 years now?) it is mostly superfluous, **certainly** in this case. – Kukeltje Feb 09 '17 at 10:36