0

I have encode method of custom renderer for UIInput.

public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    String ClientId = component.getClientId(context);
    String hint = (String) component.getAttributes().get("placeholder");
    String styleClass = (String) component.getAttributes().get("styleClass");
    String value = (String) component.getAttributes().get("value");
    ResponseWriter writer = context.getResponseWriter();
    writer.writeAttribute("name", ClientId, null);
    writer.writeAttribute("placeholder", hint, "hint");
    writer.startElement("input", component);
    writer.writeAttribute("class", styleClass, "styleClass");
    writer.writeAttribute("value", ((UIInput) component).getValue(), "value");
    writer.endElement("input");
}

I'm write startElement after I wrote 2 attributes, but it works. I.e. how startElement method does work? Does we can startElement anywhere before endElement and after endElement of previous element.

St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

1

That's surprising. writer.writeAttribute("name", ClientId, null); is supposed to throw an IllegalStateException. As for startElement and endElement, from the javadoc:

startElement... Once this method has been called, clients can call the writeAttribute() or writeURIAttribute() methods to add attributes and corresponding values. The starting element will be closed (that is, the trailing '>' character added) on any subsequent call to startElement(), writeComment(), writeText(), endElement(), endDocument(), close(), flush(), or write().

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • _writer.writeAttribute("name", ClientId, null) is supposed to throw an IllegalStateException_ Why? This example from the core JavaSerever Faces 3rd edition. _Once this method has been called, clients can call the writeAttribute() or writeURIAttribute() methods to add attributes and corresponding values._ I.e. we can write attribute before startElement invokation? – St.Antario Dec 11 '13 at 10:51
  • @St.Antario - the javadoc indicates that that is an illegal operation. Calling `writeAttribute` before any invocation of `startElement` should throw an `IllegalStateException` because there is no context for the attributes being written. It will result in invalid HTML. How is the writer supposed to know what element the attributes are being written to? I wouldn't recommend it. There's no reason to do this anyway – kolossus Dec 11 '13 at 11:56
  • @St.Antario you can check the javadoc link in the answer to confirm the behavior of `writeAttribute` – kolossus Dec 11 '13 at 11:59
  • This is example of custom rendere for `UIInput`: http://stackoverflow.com/questions/6859520/adding-custom-attribute-html5-support-to-jsf-2-0-uiinput-component in which first we write our additional attribute and after this we invoke who contains `startElement` method and there is `illegalStateException` – St.Antario Dec 11 '13 at 12:32