6

I am using the javax.faces.render.Renderer class to render my custom components. I override either encodeBegin or encodeEnd to achieve my desired output.

I would like to know when should I use either of these methods? Is there any guideline on when should encodeBegin and encodeEnd be used?

Naveen
  • 6,786
  • 10
  • 37
  • 85

1 Answers1

7

That depends on the component tree hierarchy. Key is, do you expect children? How do you want the encoded output to look like when there are children?

Usually, you use encodeBegin() if you want to encode output before children are encoded. E.g. a start tag like HTML <div>. Usually, you use encodeEnd() if you want to encode output after children are encoded. E.g. an end tag like HTML </div>. Or perhaps an additional <script> which should work on the before-generated <div>.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • **encode output before/after children are encoded?** Does this mean generating HTML like `
    span textdiv text
    `? But this can be generated even in a single method of `encodeBegin` or `encodeEnd`. Let me know if I understood you correctly.
    – Naveen Jul 10 '13 at 13:26
  • With children I mean, ``. Does it make sense now? You shouldn't care about actual encoding of children. They do all by themselves. But with `encodeBegin()` you control the output before the children are encoded and with `encodeEnd()` the output thereafter. – BalusC Jul 10 '13 at 13:29
  • Got it! :) Thanks for bearing with. I am kind off new to this JSF stuff. – Naveen Jul 11 '13 at 04:55