private void removeDuplicateResources ( UIViewRoot root , FacesContext context ) { List < UIComponent > resourcesToRemove = new ArrayList < UIComponent > ( ) ; Map < String , UIComponent > alreadyThere = new HashMap < String , UIComponent > ( ) ; List < UIComponent > components = new ArrayList < UIComponent > ( root . getComponentResources ( context , "head" ) ) ; Collections . sort ( components , new Comparator < UIComponent > ( ) { @ Override public int compare ( UIComponent o1 , UIComponent o2 ) { return o1 . getClientId ( ) . compareTo ( o2 . getClientId ( ) ) ; } } ) ; for ( UIComponent resource : components ) { String name = ( String ) resource . getAttributes ( ) . get ( "name" ) ; String library = ( String ) resource . getAttributes ( ) . get ( "library" ) ; String url = ( String ) resource . getAttributes ( ) . get ( "url" ) ; String key ; if ( null != url ) { key = url ; } else { key = library + "/" + name + "/" + resource . getClass ( ) . getName ( ) ; } if ( alreadyThere . containsKey ( key ) ) { resourcesToRemove . add ( resource ) ; continue ; } alreadyThere . put ( key , resource ) ; } for ( UIComponent c : resourcesToRemove ) { c . setInView ( false ) ; root . removeComponentResource ( context , c ) ; // String name = (String) c.getAttributes().get("name"); // String library = (String) c.getAttributes().get("library"); // String url = (String) c.getAttributes().get("url"); // System.out.println("-1" + c.getClientId() + " " + name + " " + library + " " // + url + " " + c.getClass().getSimpleName() ); } }
Remove duplicate resource files . For some reason many resource files are added more than once especially when AJAX is used . The method removes the duplicate files .