I need to add programmatically JavaScript and CSS resources to <h:head> of a JSF page. It isn’t clear how to achieve this. How can I do it or is there a kickoff example?
I need to add programmatically JavaScript and CSS resources to <h:head> of a JSF
Share
That depends on where exactly you’d like to declare the resource. Normally, the only reason to programmatically declare them is that you’ve a custom
UIComponentorRendererwhich generates HTML code which in turn requires those JS and/or CSS resources. They are then to be declared by@ResourceDependencyor@ResourceDependencies.But if you really need to declare them elsewhere, such as in a backing bean method which is invoked before render response (otherwise it’s simply too late), then you can do that by
UIViewRoot#addComponentResource(). The component resource must be created as anUIOutputhaving a renderer type ofjavax.faces.resource.Scriptorjavax.faces.resource.Stylesheet, to represent a fullworthy<h:outputScript>or<h:outputStylesheet>respectively. Thelibraryandnameattributes can just be put in the attribute map.