我在JSF 2中使用Primefaces 3來創建搜索框。我需要一個非標準的屬性(X-WebKit的語音)增加了控制,因此您將有這樣的事情......如何讓JSF通過HTML屬性
<p:autoComplete x-webkit-speech="x-webkit-speech" ... />
因爲這個屬性不是自動完成控制JSF的一部分給我500錯誤。但是,當我刪除它,頁面渲染罰款。一般來說,如何指定通過JSF標籤上的屬性來忽略它們?
我在JSF 2中使用Primefaces 3來創建搜索框。我需要一個非標準的屬性(X-WebKit的語音)增加了控制,因此您將有這樣的事情......如何讓JSF通過HTML屬性
<p:autoComplete x-webkit-speech="x-webkit-speech" ... />
因爲這個屬性不是自動完成控制JSF的一部分給我500錯誤。但是,當我刪除它,頁面渲染罰款。一般來說,如何指定通過JSF標籤上的屬性來忽略它們?
設計JSF會在呈現HTML時忽略所有自定義屬性。你需要一個自定義的渲染器。這是在PrimeFaces <p:autoComplete>
(和所有其他組件)的情況下,幸運的是相對簡單。只需要覆蓋renderPassThruAttributes()
方法就足夠了,您可以將要呈現的新屬性添加到attrs
參數中,並最終將其委託給super方法。
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);
}
}
得到它的運行,如下面進行註冊你的webapp的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
中註冊的自定義渲染器時將不起作用。
我不確定這是否可能。我會使用javascript或jQuery在客戶端添加這些屬性。
如果你想整合服務器端的東西,你可以把el表達式放到你的javascript代碼中。
這是一個討厭的黑客攻擊 –
大多數標籤都可以使用JSF-Ext中的屬性標籤進行擴展。
<html xmlns:h="http://java.sun.com/jsf/html" xmlns:e="http://java.sun.com/jsf/ext">
<!-- ... -->
<h:inputText id="name" value="#{bean.name}">
<e:attribute name="placeholder" value="My Name"/>
</h:inputText>
<!-- ... -->
</html>
您可以通過配置Maven是:
<dependency>
<groupId>com.intersult</groupId>
<artifactId>jsf-ext</artifactId>
<version>2.2.0.1</version>
</dependency>
圖書館看看這個http://stackoverflow.com/a/6675592/617373 – Daniel