如何让 JSF 传递 HTML 属性 [英] How to let JSF pass through HTML attributes

查看:26
本文介绍了如何让 JSF 传递 HTML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JSF 2 中使用 Primefaces 3 来制作搜索框.我需要向控件添加一个非标准属性 (x-webkit-speech),这样你就会有这样的东西......

I am using Primefaces 3 in JSF 2 to make a search box. I need to add a non-standard attribute (x-webkit-speech) to the control so you would have something like this...

<p:autoComplete x-webkit-speech="x-webkit-speech" ... />

由于此属性不是自动完成控件的一部分,因此 JSF 给了我 500 错误.但是当我删除它时,页面呈现良好.通常,您如何在 JSF 标记上指定传递属性以便忽略它们?

Since this attribute isn't part of the autoComplete control JSF gives me a 500 error. But when I remove it, the page renders fine. In general, how do you specify pass through attributes on a JSF tag so they are ignored?

推荐答案

JSF 的设计在呈现 HTML 时会忽略所有自定义属性.

JSF by design ignores all custom attributes when rendering HTML.

如果您已经使用 JSF 2.2+,只需将其指定为 passthrough 属性:

If you're already on JSF 2.2+, simply specify it as passthrough attribute:

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<p:autoComplete a:x-webkit-speech="x-webkit-speech" ... />

如果您还没有使用 JSF 2.2,那么您需要一个自定义渲染器.这是在 PrimeFaces (以及所有其他组件)的情况下,幸运的是相对简单.仅覆盖 renderPassThruAttributes() 方法就足够了,在该方法中,您将想要呈现的新属性添加到 attrs 参数,并最终委托给 super 方法.

If you're not on JSF 2.2 yet, then you need a custom renderer. This is in case of PrimeFaces <p:autoComplete> (and all other components) fortunately relatively simple. It's sufficient to override just the renderPassThruAttributes() method wherein you add the new attribute which you'd like to render to the attrs argument and finally delegate to the super method.

例如

package com.example;

import java.io.IOException;

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

import org.primefaces.component.autocomplete.AutoCompleteRenderer;

public class MyAutoCompleteRenderer extends AutoCompleteRenderer {

    @Override
    protected void renderPassThruAttributes(FacesContext facesContext, UIComponent component, String[] attrs) throws IOException {
        String[] newAttrs = new String[attrs.length + 1];
        System.arraycopy(attrs, 0, newAttrs, 0, attrs.length);
        newAttrs[attrs.length] = "x-webkit-speech";
        super.renderPassThruAttributes(facesContext, component, newAttrs);
    }

}

要使其运行,请在您的 web 应用程序的 faces-config.xml 中按如下方式注册它:

To get it to run, register it as follows in your webapp's faces-config.xml:

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.AutoCompleteRenderer</renderer-type>
        <renderer-class>com.example.MyAutoCompleteRenderer</renderer-class>
    </renderer>
</render-kit>

(您可以通过查看AutoComplete 类的源代码来找出组件系列和渲染器类型,它们被指定为COMPONENT_FAMILYRENDERER_TYPE 常量)

(you can find out the component family and renderer type by looking at the source code of AutoComplete class, they're specified as COMPONENT_FAMILY and RENDERER_TYPE constants in there)

不,@FacesRenderer 注释在目的是覆盖本身已经在 faces-config.xml 中注册的自定义渲染器时根本不起作用.

No, the @FacesRenderer annotation simply won't work when the purpose is to override custom renderers which are by itselves already registered in a faces-config.xml.

这篇关于如何让 JSF 传递 HTML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆