完成Component的自订,接下来要设定一个自订Tag与之对应,自订Tag的目的,在于设定 Component属性,取得Componenty型态,取得Renderer型态值等;属性的设定包括了设定静态值、设定绑定值、设定验证器等等。
要自订与Component对应的Tag,您可以继承UIComponentTag,例如:
TextWithCmdTag.java
package onlyfun.caterpillar;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.webapp.UIComponentTag;
public class TextWithCmdTag extends UIComponentTag {
private String size;
private String value;
public String getComponentType() {
return "onlyfun.caterpillar.TextWithCmd";
}
public String getRendererType() {
return null;
}
public void setProperties(UIComponent component) {
super.setProperties(component);
setStringProperty(component, "size", size);
setStringProperty(component, "value", value);
}
private void setStringProperty(UIComponent component,
String attrName, String attrValue) {
if(attrValue == null)
return;
if(isValueReference(attrValue)) {
FacesContext context =
FacesContext.getCurrentInstance();
Application application =
context.getApplication();
ValueBinding binding =
application.createValueBinding(attrValue);
component.setValueBinding(attrName, binding);
}
else {
component.getAttributes().
put(attrName, attrValue);
}
}
public void release() {
super.release();
size = null;
value = null;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;