Java – Response Writer in jsf

javajsf

ResponseWriter writer=context.getResponseWriter();

I want to know about startElement, endElement, and writeAttribute methods on ResponseWriter.

Best Solution

JSF output is HTML/XML and a ResponseWriter makes it easier to generate it correctly. Say you wanted to render some text in a <span> tag.

<span>My random text</span>

The code would look like:

ResponseWriter writer=context.getResponseWriter();
writer.startElement("span", component);
writer.writeText(text, null);
writer.endElement("span");

writerAttribute comes in when you need to add an ID or class attribute to the tag.

ResponseWriter writer=context.getResponseWriter();
writer.startElement("span", component);
writer.writeAttribute("id", id, null);
writer.writeText(text, null);
writer.endElement("span");

This would render:

<span id="myId">My text</span>

Note: writeAttribute immediately follows startElement. Once you start another element or write some text, you cannot call writeAttribute.