如何让JSF通过HTML属性 [英] How to let JSF pass through HTML attributes
问题描述
我在JSF 2中使用Primefaces 3来创建一个搜索框。我需要在控件中添加一个非标准属性(x-webkit-speech),这样你就可以拥有像这样的内容......
< p:autoComplete x-webkit-speech =x-webkit-speech... />
由于此属性不是autoComplete控件的一部分,因此JSF会给我一个500错误。但是,当我删除它,页面渲染罚款。一般来说,如何指定在JSF标记上传递属性以便忽略它们?
解析方案 设计JSF忽略所有自定义呈现HTML时的属性。你需要一个自定义的渲染器。 PrimeFaces < p:autoComplete>
(以及所有其他组件)的情况相当简单。只需重写 renderPassThruAttributes()
方法即可,其中您将要呈现的新属性添加到 attrs
参数,最后委托给super方法。
例如
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 {
$ b $ @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
中如下所示:
< ;渲染试剂盒GT;
<渲染器>
< component-family> org.primefaces.component< / component-family>
< renderer-type> org.primefaces.component.AutoCompleteRenderer< / renderer-type>
< renderer-class> com.example.MyAutoCompleteRenderer< / renderer-class>
< /渲染器>
< / render-kit>
(您可以通过查看源代码来了解组件系列和渲染器类型 AutoComplete
类,它们被指定为 COMPONENT_FAMILY
和 RENDERER_TYPE
常量在那里)
不, @FacesRenderer
是覆盖自己已经注册在 faces-config.xml
中的自定义渲染器。
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" ... />
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 by design ignores all custom attributes when rendering HTML. 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.
E.g.
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);
}
}
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>
(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)
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屋!