I asked about pass through attributes in a different question and found I could create a custom renderer for the <p:autocomplete> component but the problem is my custom renderer would be used for every p:autocomplete in my project (site-wide). Therefore I have elected to create a custom component which extends org.primefaces.component.autocomplete.AutoComplete and adds the necessary attributes to the text box.
My initial thought was to add a constructor but it doesn’t seem to work because the attribute map is null at this point:
@FacesComponent("com.mycomponents.SiteSearch")
public class SiteSearch extends AutoComplete {
public SiteSearch() {
Map<String,Object> attrs = getAttributes();
attrs.put("x-webkit-speech", null);
attrs.put("x-webkit-grammer", "builtin:search");
attrs.put("onwebkitspeechchange", "this.form.submit();");
attrs.put("placeholder", "Enter a Search Term");
}
}
My other thought was leave this custom component empty (empty class) and then specify a custom renderer that extends org.primefaces.component.autocomplete.AutoCompleteRenderer and modify the attributes there.
After all is said and done, I just need a way to keep these attributes separate to this one text box so just putting a custom renderer on the p:autoComplete is not going to work (unless maybe I can use renderType= attribute for this one p:autoComplete?).
If you need a specific component which uses a different renderer than
<p:autoComplete>then you really can’t go around creating a custom component with its own family and component type. You can still just extend the PrimeFacesAutoComplete(and its renderer) to save some boilerplate code.In the custom component, you need to provide getters for those attributes. You could as good specify setters as well, this way you can always override the default values from in the view side. Those getters/setters should in turn delegate to
StateHelper.There’s only a little problem with
x-webkit-*attributes. The-is an illegal character in Java identifiers. So you have to rename the getters/setters and change the renderer somewhat as the standard renderer relies on the component property name being exactly the same as the tag attribute name. Update: I understand thatx-webkit-speechshould just be rendered as is (so, no getter/setter necessary) and thatx-webkit-grammeris actually a typo, it should bex-webkit-grammar.Here’s how the
SiteSearchcomponent can look like:Please note that the getters have all default values specified. If the
eval()returnsnull, then the default value will be returned instead. I have also neutralized the attribute names somewhat so that it can be reused for any future non-webkit browsers by just modifying the renderer accordingly.And here’s how the
SiteSearchRendererrenderer should look like for the above component:To use it in the view, we of course need to register it as a tag. Create a
/WEB-INF/my.taglib.xmlfile:Note that you don’t need a
<renderer>infaces-config.xmlfor this anymore. The@FacesRendererannotation can just do its job on real custom components. So remove the<renderer>entry infaces-config.xmlwhich you created based on your previous question.Now tell JSF that you’ve got a custom taglib by the following context param in
web.xml:Finally you can use it as follows:
You can even specify additional attributes which will override the defaults set in the component:
For IDE autocomplete on attributes, you’d need to specify every one as a separate
<attribute>in the<tag>declaration in themy.taglib.xml.