Search code examples
oracle-databaseoracle-adfjdeveloper

Add a <script> tag to the <head> section from a partial loaded via af:declarativeComponent in Oracle ADF


I have an ADF project (JDeveloper 11) with a few pages. All pages load a partial file that holds the top navigation bar, like this:

mainFile.jspx

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">

  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1"">

      <af:declarativeComponent id="dc0" viewId="/partials/header-small.jspx" />
....

In the partial file I have this:

header-small.jspx

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">

  <jsp:directive.page contentType="text/html;charset=UTF-8"/>

    <link rel="stylesheet" href="some-stylesheet-file.css"/>

    <script src="javascripts/modernizr.js"></script>
    <script src="javascripts/fonts.js"></script>

    <header class="top_header" role="presentation">
....

As it is now, the link and script tags are added in the <body> section, but I want them in the <head>. I couldn't find anywhere a proper way to do this. Is it even possible?

I can't implement templates at this point, because the project was created by someone else that is unavailable, and I'm only in charge of the front-end.


Solution

  • To get the script and the link inside the <head> tag, I am afraid you will need to place your style sheet and javascript resources inside mainFile.jspx:

     <af:document>
       <af:resource type="javascript" source="../javascripts/modernizr.js">
       <af:resource type="javascript" source="../some-stylesheet-file.css">
      <af:form> 
       ...
      </af:form> 
    </af:document>
    

    So, you won't probably be able to get a high degree of encapsulation. More details, here: https://blogs.oracle.com/jdevotnharvest/entry/gotcha_when_using_javascript_in