In order to
write and configure a ComponentHandler,
you have to accomplish several steps:
·
First, you need a component (built-in, or
custom). Let's suppose that we have the below dummy component:
package custom.componenthandler;
import
javax.faces.component.FacesComponent;
import
javax.faces.component.UIComponentBase;
@FacesComponent(value=DummyComponent.COMPONENT_TYPE)
public class DummyComponent extends
UIComponentBase {
public static final String COMPONENT_FAMILY =
"dummy.components";
public static final String COMPONENT_TYPE =
"dummy";
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
}
·
Further, extend the ComponentHandler class and override the
desired methods. Per example, a dummy component handler skeleton is listed below
- it simply delegates tasks to the superclass:
package custom.componenthandler;
import java.util.logging.Logger;
import
javax.faces.component.UIComponent;
import
javax.faces.view.facelets.ComponentConfig;
import javax.faces.view.facelets.ComponentHandler;
import
javax.faces.view.facelets.FaceletContext;
public class CustomComponentHandler
extends ComponentHandler {
private static final Logger logger =
Logger.getLogger(CustomComponentHandler.class.getName());
public CustomComponentHandler(ComponentConfig
config) {
super(config);
}
//only in JSF 2.2
@Override
public UIComponent
createComponent(FaceletContext ctx) {
logger.info("Inside 'createComponent' method");
return super.createComponent(ctx);
}
@Override
public void onComponentCreated(FaceletContext
ctx, UIComponent c, UIComponent parent) {
logger.info("Inside 'onComponentCreated' method");
super.onComponentCreated(ctx, c, parent);
}
@Override
public void
onComponentPopulated(FaceletContext ctx, UIComponent c, UIComponent parent) {
logger.info("Inside
'onComponentPopulated' method");
super.onComponentPopulated(ctx, c, parent);
}
}
·
Next, you need to write a *.taglib.xml file to link
the handler class with the component. Per example, dummy.taglib.xml from below:
<?xml version="1.0"
encoding="UTF-8"?>
<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://dummy.components.org/dummy</namespace>
<tag>
<tag-name>dummy</tag-name>
<component>
<component-type>dummy</component-type>
<handler-class>custom.componenthandler.CustomComponentHandler</handler-class>
</component>
<attribute>
<name>id</name>
<type>java.lang.String</type>
<description>Component id</description>
<required>false</required>
</attribute>
<!--
... -->
</tag>
</facelet-taglib>
·
Finally, configure the *taglib.xml file in web.xml file. Per example:
...
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/tags/dummy.taglib.xml</param-value>
</context-param>
...
Done! The
complete code is available on GitHub.
Niciun comentariu :
Trimiteți un comentariu