2012-09-16 8 views
6

मुझे जेएसएफ पृष्ठ के <h:head> पर प्रोग्रामेटिक जेएस और सीएसएस संसाधनों को जोड़ने की आवश्यकता है। यह स्पष्ट नहीं है कि इसे कैसे प्राप्त किया जाए। क्या कोई संकेत या किकऑफ उदाहरण दे सकता है?प्रोग्रामिंग रूप से जेएस और सीएसएस संसाधनों को <h:head> पर कैसे जोड़ें?

उत्तर

11

वहीं पर निर्भर करता है वास्तव में आप यह घोषणा करना चाहते हैं संसाधन। आम तौर पर, प्रोग्रामेटिक रूप से उन्हें घोषित करने का एकमात्र कारण यह है कि आपके पास कस्टम UIComponent या Renderer है जो HTML कोड उत्पन्न करता है जिसके बदले में उन जेएस और/या सीएसएस संसाधनों की आवश्यकता होती है। उन्हें या @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 { 
    // ... 
} 

लेकिन अगर आप वास्तव में इस तरह के एक समर्थन सेम विधि है जो शुरू हो जाती है से पहले प्रतिक्रिया प्रस्तुत करना के रूप में, कहीं और उन्हें घोषित करने के लिए की जरूरत है (अन्यथा यह बस बहुत देर हो चुकी है), तो आप ऐसा कर सकते हैं UIViewRoot#addComponentResource() द्वारा। घटक संसाधन को UIOutput के रूप में बनाया जाना चाहिए जो javax.faces.resource.Script या javax.faces.resource.Stylesheet का एक रेंडरर प्रकार है, जो क्रमशः <h:outputScript> या <h:outputStylesheet> का प्रतिनिधित्व करने के लिए है। library और name विशेषताएँ केवल विशेषता मानचित्र में रखी जा सकती हैं।

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"); 
+1

यहां आपको जानकारी कहां मिलनी है: http://stackoverflow.com/questions/3586629 – Thor

+0

ग्रेट! यह मेरा दिन बचाया। –

+0

मैं घोषणा को जोड़ने के लिए भी संघर्ष कर रहा था। मैंने इसे अपने यूआईसीओम्पोनेंट के निर्माता में जोड़ दिया। –

2

आप इस तरह एक पृष्ठ पर एक स्क्रिप्ट और शैली संसाधनों जोड़ सकते हैं:

var head = document.getElementsByTagName("head")[0]; 
var s = document.createElement("script"); 
s.type = "text/javascript"; 
s.src = "xxxx.js"; 
head.appendChild(s); 

s = document.createElement("style"); 
s.type = "text/css" 
s.src = "yyy.css"; 
head.appendChild(s); 

या, समारोह के रूप में:

function addScript(path) { 
    var head = document.getElementsByTagName("head")[0]; 
    var s = document.createElement("script"); 
    s.type = "text/javascript"; 
    s.src = path; 
    head.appendChild(s); 
} 

function addCSSFile(path) { 
    var head = document.getElementsByTagName("head")[0]; 
    var s = document.createElement("style"); 
    s.type = "text/css"; 
    s.src = path; 
    head.appendChild(s); 
} 
+0

हालांकि यह जावास्क्रिप्ट में उपयोग किए जाने पर मान्य है, यह जेएसएफ संदर्भ में मदद नहीं करता है। –

संबंधित मुद्दे