You may also want to see:
JSF 2.2 - Write Custom Renderer Skeleton
JSF 2.2 - Write Custom RenderKit Skeleton
JSF 2.2 - Write Custom Renderer Skeleton
JSF 2.2 - Write Custom RenderKit Skeleton
What is the main goal of a RenderKitFactory
?
·
it manages (register/provide) instances of available
RenderKits.
When you commonly need a custom RenderKitFactory
?
·
you may use a custom RenderKitFactory to
instruct JSF to delegate RenderKits in a specific approach. By
default, JSF has a single RenderKit, identified by an ID, HTML_BASIC_RENDER_KIT.
But, if want to write another RenderKit then you can programmatically
choose between them using a custom RenderKitFactory.
What should I know before start writing a
custom RenderKitFactory ?
·
mainly you need to know that a render kit
factory extends directly/indirectly the RendererKitFactory class
·
The main two methods of a RenderKitFactory
are addRenderKit()and
getRenderKit()
. By overriding these methods, you can take control over the RenderKits8
registration and delegation. The render kits IDs can be obtained via getRenderKitIds()
method. If this factory has been decorated, the implementation doing the
decorating may override the getWrapped() method to provide access to the
implementation being wrapped.
·
in order to link a component with a renderer,
you should know how to work with the UIComponent.setRenderType() method and
with the component-family,
component-type
and renderer-type
artifacts as level of annotations or with the <render-kit> and <renderer> tag in faces-config.xml.
A detailed dissertation about them is the OmniFaces
Custom Components Approach post. OmniFaces provides solid techniques for
writing custom components and renderers!
How do I usually write a RenderKitFactory
skeleton ?
·
usually, you will extend the RenderKitFactory
class, override the necessary methods, and configure it in the faces-config.xml
via <render-kit-factory>
tag
e.g. replace the default JSF RenderKit with the
DummyRenderKit - GitHub Complete Code [CustomRenderKitFactorySkeleton]
DummyRenderKitFactory
public class
DummyRenderKitFactory extends RenderKitFactory {
private static final Logger LOG =
Logger.getLogger(DummyRenderKitFactory.class.getName());
private RenderKitFactory renderKitFactory;
public DummyRenderKitFactory() {
}
public DummyRenderKitFactory(RenderKitFactory
renderKitFactory) {
this.renderKitFactory = renderKitFactory;
}
@Override
public void addRenderKit(String renderKitId,
RenderKit renderKit) {
LOG.log(Level.INFO, "Register RenderKit
with ID: {0}", renderKitId);
getWrapped().addRenderKit(renderKitId,
renderKit);
}
@Override
public RenderKit getRenderKit(FacesContext
context, String renderKitId) {
LOG.log(Level.INFO, "Delegate
DummyRenderKit instead of RenderKit with ID: {0}", renderKitId);
RenderKit renderKit =
getWrapped().getRenderKit(context, renderKitId);
return
(HTML_BASIC_RENDER_KIT.equals(renderKitId)) ? new DummyRenderKit(renderKit) : renderKit;
}
@Override
public Iterator<String>
getRenderKitIds() {
return getWrapped().getRenderKitIds();
}
@Override
public RenderKitFactory getWrapped() {
return renderKitFactory;
}
}
DummyRenderKit
public class
DummyRenderKit extends RenderKitWrapper {
private static final Logger LOG =
Logger.getLogger(DummyRenderKit.class.getName());
private RenderKit renderKit;
public DummyRenderKit() {
}
public DummyRenderKit(RenderKit renderKit) {
LOG.log(Level.INFO, "Default RenderKit
instance provided by JSF: {0}", renderKit.getClass().getSimpleName());
this.renderKit = renderKit;
}
@Override
public Renderer getRenderer(String family,
String rendererType) {
LOG.log(Level.INFO, "--- Delegating
renderer of type {0} for component in family {1} ---", new
Object[]{rendererType, family});
return getWrapped().getRenderer(family,
rendererType);
}
@Override
public void addRenderer(String family, String
rendererType, Renderer renderer) {
LOG.log(Level.INFO, "--- Adding the
renderer of type {0} for component in family {1} ---", new
Object[]{rendererType, family});
getWrapped().addRenderer(family,
rendererType, renderer);
}
@Override
public RenderKit getWrapped() {
return renderKit;
}
}
faces-config.xml
...
<factory>
<render-kit-factory>
jsf.renderkitfactories.DummyRenderKitFactory
</render-kit-factory>
</factory>
...
Niciun comentariu :
Trimiteți un comentariu