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

  • SEARCH
  • Home
  • 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 8535501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:26:47+00:00 2026-06-11T10:26:47+00:00

I need to add programmatically JavaScript and CSS resources to <h:head> of a JSF

  • 0

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?

  • 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-11T10:26:48+00:00Added an answer on June 11, 2026 at 10:26 am

    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 UIComponent or Renderer which generates HTML code which in turn requires those JS and/or CSS resources. They are then to be declared by @ResourceDependency or @ResourceDependencies.

    @ResourceDependency(library="mylibrary", name="foo.css")
    public class FooComponentWithCSS extends UIComponentBase {
        // ...
    }
    
    @ResourceDependencies({
        @ResourceDependency(library="mylibrary", name="bar.css"),
        @ResourceDependency(library="mylibrary", name="bar.js")
    })
    public class BarComponentWithCSSandJS extends UIComponentBase {
        // ...
    }
    

    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 an UIOutput having a renderer type of javax.faces.resource.Script or javax.faces.resource.Stylesheet, to represent a fullworthy <h:outputScript> or <h:outputStylesheet> respectively. The library and name attributes can just be put in the attribute map.

    UIOutput css = new UIOutput();
    css.setRendererType("javax.faces.resource.Stylesheet");
    css.getAttributes().put("library", "mylibrary");
    css.getAttributes().put("name", "bar.css");
    
    UIOutput js = new UIOutput();
    js.setRendererType("javax.faces.resource.Script");
    js.getAttributes().put("library", "mylibrary");
    js.getAttributes().put("name", "bar.js");
    
    FacesContext context = FacesContext.getCurrentInstance();
    context.getViewRoot().addComponentResource(context, css, "head");
    context.getViewRoot().addComponentResource(context, js, "head");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't access a element with its href. Like this example, i need add
need to add a 2nd css stylesheet to a page. do i add a
I need add Auto complete on apex Tabular Form. there is a column as
We need to add a javascript element inside an iframe (its inside the same
I need to programmatically add additional graphical elements onto an existing, static PDF book
I want to add StyleSheets programmatically in the head section but one of the
I know I can use the wp_insert_post() function in WordPress to add posts programmatically,
Is there a way that I can Programmatically set an Expires Header in code
I need to programmatically add a set of controls with some amount of pixels
I have a User Control that I need to programmatically add to a Silverlight

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.