My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

miercuri, 4 februarie 2015

JSF 2.2 - Write Custom RenderKitFactory 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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors