Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
覆盖Primefaces渲染器_Primefaces_Jsf 2 - Fatal编程技术网

覆盖Primefaces渲染器

覆盖Primefaces渲染器,primefaces,jsf-2,Primefaces,Jsf 2,我正在使用Primefaces和JSF2.2开发一个动态菜单 问题是它没有插入菜单id。查看primefaces的代码时,它会遇到一个始终为false的代码: BaseMenUnderer: protected boolean shouldRenderId(MenuElement element) { if(element instanceof UIComponent) return shouldWriteId((UIComponent) element); el

我正在使用Primefaces和JSF2.2开发一个动态菜单

问题是它没有插入菜单id。查看primefaces的代码时,它会遇到一个始终为false的代码:

BaseMenUnderer:

protected boolean shouldRenderId(MenuElement element) {
    if(element instanceof UIComponent)
        return shouldWriteId((UIComponent) element);
    else
        return false;
}
 writer.startElement("li", null);
                    if(shouldRenderId(submenu)) {
                        writer.writeAttribute("id", submenu.getClientId(), null);
                    }
分层菜单详细信息:

protected boolean shouldRenderId(MenuElement element) {
    if(element instanceof UIComponent)
        return shouldWriteId((UIComponent) element);
    else
        return false;
}
 writer.startElement("li", null);
                    if(shouldRenderId(submenu)) {
                        writer.writeAttribute("id", submenu.getClientId(), null);
                    }
因此,我决定重写primefaces的TieredMenuRenderer,但是调用了我的重写构造函数classe,但从未调用过重写方法

下面是我如何设置facesconfig.xml的

<render-kit>
        <renderer>
            <component-family>org.primefaces.component</component-family>
            <renderer-type>org.primefaces.component.TieredMenuRenderer</renderer-type>
            <renderer-class>ui.jsf.TieredMenuRenderer</renderer-class>
        </renderer>
    </render-kit>
添加菜单xhtml,“menucontroller.model”是一个primefaces菜单模型,我使用的是DefaultMenuModel:

在执行以下命令时,在renderer kit上迭代,返回正确的渲染器类型
org.primefaces.component.tieredmenurender

Iterator<String> renderKit = kit.getRendererTypes("org.primefaces.component");

首先,我的渲染器没有覆盖正确的渲染器。当我应该重写
org.primefaces.component.tieredmenu.TieredMenuRenderer时,我正在重写
org.primefaces.component.MenubarRenderer

然后,为了纠正primefaces id问题,我在渲染类中执行了以下操作:

 @Override
    protected boolean shouldRenderId(MenuElement element) {
        return true;
    }

@Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        AbstractMenu menu = (AbstractMenu) component;
        encodeMarkup(context, menu);
        encodeScript(context, menu);
    }
import java.io.IOException;

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

import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.model.menu.MenuElement;

public class MenuRenderer extends org.primefaces.component.menubar.MenubarRenderer {

    @Override
    protected boolean shouldRenderId(MenuElement element) {
        return true;
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        AbstractMenu menu = (AbstractMenu) component;
        encodeMarkup(context, menu);
        encodeScript(context, menu);
    }

}
EncodedEnd从primefaces调用“generateIds()”,primefaces覆盖了我的ID

完整渲染类:

 @Override
    protected boolean shouldRenderId(MenuElement element) {
        return true;
    }

@Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        AbstractMenu menu = (AbstractMenu) component;
        encodeMarkup(context, menu);
        encodeScript(context, menu);
    }
import java.io.IOException;

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

import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.model.menu.MenuElement;

public class MenuRenderer extends org.primefaces.component.menubar.MenubarRenderer {

    @Override
    protected boolean shouldRenderId(MenuElement element) {
        return true;
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        AbstractMenu menu = (AbstractMenu) component;
        encodeMarkup(context, menu);
        encodeScript(context, menu);
    }

}

包“ui.jsf.tieredmenurender”与类的包匹配吗?另外,您的方法的方法签名是否完全匹配?是的。我的类使用EclipseIDE中的“复制限定名”。我用autocomplete重写了这个方法。啊,我想我刚刚注意到你的渲染器类型类名和你要扩展的类名不匹配。在facesconfig.xml中,包路径中缺少“.tieredmenu”。在所有示例中,我都是这样发现的。此外,我还从primefaces的faces-config.xml本身复制了这种渲染器类型。但是我会尝试一下…当为菜单调用
javax.faces.component.UIComponentBase.getRenderer(FacesContext)
时,检查实际使用的渲染器。