Tuesday, August 14, 2012

Programmatically Add JavaScript/Stylesheet to component tree

Declaratively, you can use h:outputScript and h:outputStylesheet. But there are no corresponding components for these tags in JSF.

I find how to do this programmatically by tracing into JSF RI.

Add javascript.
 UIComponent component=ftc.getApplication().createComponent(ftc,   UIOutput.COMPONENT_TYPE, "javax.faces.resource.Script");
            component.getAttributes().put("library",library);
            component.getAttributes().put("name", "mytest.js");
            component.getAttributes().put("target", "body");
            container.getChildren().add(component);

Add stylesheet
UIComponent component=ftc.getApplication().createComponent(ftc, UIOutput.COMPONENT_TYPE, "javax.faces.resource.Stylesheet");
            component.getAttributes().put("library",library);
            component.getAttributes().put("name", "test.css");
            component.getAttributes().put("target", "head");
            container.getChildren().add(component);


No comments:

Post a Comment