<分区>
我在 JSF 2 中使用 Primefaces 3 制作搜索框。我需要向控件添加一个非标准属性(x-webkit-speech),这样你就会有这样的东西......
<p:autoComplete x-webkit-speech="x-webkit-speech" ... />
因为这个属性不是自动完成控件的一部分,JSF 给我一个 500 错误。但是当我删除它时,页面呈现正常。通常,您如何在 JSF 标记上指定传递属性以使其被忽略?
<分区>
我在 JSF 2 中使用 Primefaces 3 制作搜索框。我需要向控件添加一个非标准属性(x-webkit-speech),这样你就会有这样的东西......
<p:autoComplete x-webkit-speech="x-webkit-speech" ... />
因为这个属性不是自动完成控件的一部分,JSF 给我一个 500 错误。但是当我删除它时,页面呈现正常。通常,您如何在 JSF 标记上指定传递属性以使其被忽略?
最佳答案
JSF 在设计时会在呈现 HTML 时忽略所有自定义属性。
如果您已经在使用 JSF 2.2+,只需将其指定为 passthrough attribute :
<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<p:autoComplete a:x-webkit-speech="x-webkit-speech" ... />
如果您还没有使用 JSF 2.2,那么您需要一个自定义渲染器。这是针对 PrimeFaces <p:autoComplete>
的情况(和所有其他组件)幸运的是相对简单。仅覆盖 renderPassThruAttributes()
就足够了方法,其中您将要呈现的新属性添加到 attrs
参数并最终委托(delegate)给 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 {
@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);
}
}
要让它运行,请按以下方式在您的网络应用程序的 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_FAMILY
和 RENDERER_TYPE
常量)
不,@FacesRenderer
当目的是覆盖自定义渲染器时,注释根本不起作用,这些渲染器本身已经在 faces-config.xml
中注册了。 .
关于html - 如何让JSF通过HTML属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484337/
相关文章:
javascript - Chrome 扩展程序弹出按钮在第二次按下后没有变化
javascript - 使用 JSON、HTML 和 JavaScript 更改网站语言
html - 如何轻松保存我的 Rails 应用程序的 HTML 页面,以提供给 Designer?
java - 是否可以降级 JSF 中的 NamingContainer?
java - 重新访问 View 时清除 session 作用域 bean 的数据
jsf-2 - Primefaces lazy datascroller 调用加载两次