如何用自定义渲染器覆盖基本渲染器

如何用自定义渲染器覆盖基本渲染器

本文介绍了JSF 2.0:如何用自定义渲染器覆盖基本渲染器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为h:selectBooleanCheckbox覆盖渲染器(出于此处):

I am trying to override renderer for h:selectBooleanCheckbox (for the reasons explained here):

但是,我发现无法注册渲染器.我尝试在我的faces-config.xml中声明它:

However, I find it impossible to register my renderer. I have tried declaring it in my faces-config.xml:

<render-kit>
    <renderer>
        <component-family>javax.faces.SelectBoolean</component-family>
        <renderer-type>javax.faces.Checkbox</renderer-type>
        <renderer-class>com.myapp.CustomCheckboxRenderer</renderer-class>
    </renderer>
</render-kit>

我从中获取的价值:

  • 组件家族:javax.faces.component.html.HtmlSelectBooleanCheckbox
  • 渲染器类型:javax.faces.component.html.SelectBooleanCheckboxTag
  • component-family: javax.faces.component.html.HtmlSelectBooleanCheckbox
  • renderer-type: javax.faces.component.html.SelectBooleanCheckboxTag

但这是行不通的.

我也尝试过详细声明RenderKit:

<description>Custom renderers</description>
<render-kit-id>???</render-kit-id>
<render-kit-class>com.sun.faces.renderkit.RenderKitImpl</render-kit-class>

但是正如您所看到的,我真的不知道在哪里获取render-kit-id的值,或者render-kit-class是否正确.

But as you can see, I don't really know where to grab value for render-kit-id or if the render-kit-class is correct anyway.

在Mojarra软件包中有文件jsf-ri-runtime.xml,但未声明渲染器.它只声明一个RenderKitFactory,在此之下我没有直接找到任何感兴趣的东西.

Inside Mojarra package there is file jsf-ri-runtime.xml but it doesn't declare the renderers. It only declares a RenderKitFactory, under which I don't directly find anything of interest.

指针?

推荐答案

您最初的<renderer>声明看起来不错,所以我在这里尝试过.

Your initial <renderer> declaration looks fine, so I tried it here.

package com.myapp;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import com.sun.faces.renderkit.html_basic.CheckboxRenderer;

public class CustomCheckboxRenderer extends CheckboxRenderer {

    public CustomCheckboxRenderer() {
        System.out.println("CustomCheckboxRenderer <init>");
    }

    @Override
    public void decode(FacesContext context, UIComponent component) {
        System.out.println("CustomCheckboxRenderer decode()");
        super.decode(context, component);
    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        System.out.println("CustomCheckboxRenderer encodeBegin()");
        super.encodeBegin(context, component);
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        System.out.println("CustomCheckboxRenderer encodeEnd()");
        super.encodeEnd(context, component);
    }

}

工作正常.所有都打印到标准输出.您的问题出在其他地方.我在Tomcat 7.0.5上使用的是Mojarra 2.0.3.

It works fine. All get printed to stdout. Your problem lies somewhere else. I was using Mojarra 2.0.3 on Tomcat 7.0.5.

这篇关于JSF 2.0:如何用自定义渲染器覆盖基本渲染器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:34