问题
I've had occasion to write some custom renderers for my project and that's working perfectly well. However I am somewhat confused by some of the parameters in the ResponseWriter methods. The documentation doesn't explain this very well so I'm hoping one of the resident JSF experts can explain this better. Specifically:
public abstract void startElement(java.lang.String name,
javax.faces.component.UIComponent component)
throws java.io.IOException
Parameters:
name - Name of the element to be started
component - The UIComponent (if any) to which this element corresponds
What does that second parameter actually do? It seems to work fine whether i pass "null" or "this" in my renderer?
Similarly for writeAttribute:
public abstract void writeAttribute(java.lang.String name,
java.lang.Object value,
java.lang.String property)
throws java.io.IOException
Parameters:
name - Attribute name to be added
value - Attribute value to be added
property - Name of the property or attribute (if any) of the UIComponent associated with the containing element, to which this generated attribute corresponds
Why does the ResponseWriter need to know the backing property? Again, it seems to work fine if I pass null or, "styleClass" when writing the class attribute.
Curious minds want to know, and my google-fu is failing on this one...
回答1:
The standard Mojarra implementation does nothing with them. The component
argument of startElement()
and the property
argument of writeAttribute()
are plain ignored.
However, it's possible to provide a custom response writer. For some real world implementations it would make completely sense to know about the originating UIComponent
and/or the associated UIComponent
property inside the response writer.
Although JSF 2.0 targeted, the Html5ResponseWriter of OmniFaces would be a good example. The startElement()
determines the type of the UIComponent
by several instanceof
checks before allowing/writing some specific HTML5 attributes.
来源:https://stackoverflow.com/questions/11810219/jsf-1-2-startelement-and-writeattribute-explanation