Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:54:13+00:00 2026-06-03T18:54:13+00:00

I asked about pass through attributes in a different question and found I could

  • 0

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?).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:54:19+00:00Added an answer on June 3, 2026 at 6:54 pm

    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 PrimeFaces AutoComplete (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 that x-webkit-speech should just be rendered as is (so, no getter/setter necessary) and that x-webkit-grammer is actually a typo, it should be x-webkit-grammar.

    Here’s how the SiteSearch component can look like:

    @FacesComponent(SiteSearch.COMPONENT_TYPE)
    public class SiteSearch extends AutoComplete {
    
        public static final String COMPONENT_FAMILY = "com.example";
        public static final String COMPONENT_TYPE = "com.example.SiteSearch";
    
        private enum PropertyKeys {
            grammar, onspeechchange, placeholder
        }
    
        @Override
        public String getFamily() {
            return COMPONENT_FAMILY;
        }
    
        @Override
        public String getRendererType() {
            return SiteSearchRenderer.RENDERER_TYPE;
        }
    
        public String getGrammar() {
            return (String) getStateHelper().eval(PropertyKeys.grammar, "builtin:search");
        }
    
        public void setGrammar(String grammar) {
            getStateHelper().put(PropertyKeys.grammar, grammar);
        }
    
        public String getOnspeechchange() {
            return (String) getStateHelper().eval(PropertyKeys.onspeechchange, "submit()");
        }
    
        public void setOnspeechchange(String onspeechchange) {
            getStateHelper().put(PropertyKeys.onspeechchange, onspeechchange);
        }
    
        public String getPlaceholder() {
            return (String) getStateHelper().eval(PropertyKeys.placeholder, "Enter a Search Term");
        }
    
        public void setPlaceholder(String placeholder) {
            getStateHelper().put(PropertyKeys.placeholder, placeholder);
        }
    
    }
    

    Please note that the getters have all default values specified. If the eval() returns null, 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 SiteSearchRenderer renderer should look like for the above component:

    @FacesRenderer(
        componentFamily=SiteSearch.COMPONENT_FAMILY,
        rendererType=SiteSearchRenderer.RENDERER_TYPE
    )
    public class SiteSearchRenderer extends AutoCompleteRenderer {
    
        public static final String RENDERER_TYPE = "com.example.SiteSearchRenderer";
    
        @Override
        protected void renderPassThruAttributes(FacesContext facesContext, UIComponent component, String[] attrs) throws IOException {
            ResponseWriter writer = facesContext.getResponseWriter();
            writer.writeAttribute("x-webkit-speech", "x-webkit-speech", null);
            writer.writeAttribute("x-webkit-grammar", component.getAttributes().get("grammar"), "grammar");
            writer.writeAttribute("onwebkitspeechchange", component.getAttributes().get("onspeechchange"), "onspeechchange");
            writer.writeAttribute("placeholder", component.getAttributes().get("placeholder"), "placeholder");
            super.renderPassThruAttributes(facesContext, component, attrs);
        }
    
    }
    

    To use it in the view, we of course need to register it as a tag. Create a /WEB-INF/my.taglib.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <facelet-taglib
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
        version="2.0"
    >
        <namespace>http://example.com/ui</namespace>
    
        <tag>
            <tag-name>siteSearch</tag-name>
            <component>
                <component-type>com.example.SiteSearch</component-type>
                <renderer-type>com.example.SiteSearchRenderer</renderer-type>
            </component>
        </tag>
    </facelet-taglib>
    

    Note that you don’t need a <renderer> in faces-config.xml for this anymore. The @FacesRenderer annotation can just do its job on real custom components. So remove the <renderer> entry in faces-config.xml which 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:

    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/my.taglib.xml</param-value>
    </context-param>
    

    Finally you can use it as follows:

    <html ... xmlns:my="http://example.com/ui">
    ...
    <my:siteSearch />
    

    You can even specify additional attributes which will override the defaults set in the component:

    <my:siteSearch grammar="builtin:language" onspeechchange="alert('peek-a-boo')" placeholder="Search" />
    

    For IDE autocomplete on attributes, you’d need to specify every one as a separate <attribute> in the <tag> declaration in the my.taglib.xml.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A similar question was asked about a year ago, and wasn't quite resolved, but
I have already asked about this for android, but I will broaden the question
I know the question about measuring developer performance has been asked to death, but
In asking a question about reflection I asked: Nice answer. But there is a
Yesterday I asked about serving byte ranges from PHP. Today my question is -
I've asked about strtolower function. But when using foreign characters it doesn't convert them
This stems from a previous question I asked - about a write conflict with
I asked a question yesterday about password safety... I am new at security... I
I recently asked a question about using the Page.User or HttpContext.Current.User in the View.
I asked about this yesterday , but I'm still having problems. I wrote a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.