Search code examples
gatein

JS dependencies in gatein-resources.xml


I am trying to reference JS scrips for each portlet in gatein-resources.xml. https://docs.jboss.org/author/display/GTNPORTAL34/GDG-JavaScript+Resource+Management How do I reference them to each portlet? I am trying to use the portlet scope but not sure how to reference them. Is it by name or by path? ie

in example.jsp

<script src="<%=request.getContextPath()%>/js/lib/modules/pagination.js"></script>

in gatein-resources.xml

   <portlet>
  <name>/jsp/example.jsp</name>
  <module>
   <script>
    <name>pagination</name>
    <path>/js/lib/modules/pagination.js</path>
    </script>
  </module>
  </portlet>

Edit:

If I just want to add all the javascript resources indepently from which portlet uses them, can I just add all of them like the below snippet? (I have multiple jsp files sharing different javascript files). Just trying to minimize amount of code/not sure which portlet uses which jsp file so just trying to add all of them at once. Do these jsp files need to be added to portlet.xml? I am confused about the difference between these jsp files and the .xml portlets in portlet.xml. Are these jsp files portlets as well? Sorry for my lack of understanding.

<scripts>
<name>first_resource</name> 
<script>
<name>ABC_script</name> 
<path>/abc.js</path> 
</script>
<name>second_resource</name>
<script>
<name>XYZ_script</name> 
<path>/xyz.js</path>
</script>
</scripts>

or maybe also add the tag for all the scripts listed above. Resource: shared scope in https://docs.jboss.org/author/display/GTNPORTAL34/GDG-JavaScript+Resource+Management

Thanks!


Solution

  • You need to provide the portlet name, i.e. the name in the portlet descriptor (WEB-INF/portlet.xml). It should be something like below snippet:

    <?xml version="1.0" encoding="UTF-8"?>
    <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
        http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
    
      <portlet>
        <description xml:lang="EN">A sample portlet</description>
        <portlet-name>MyPortletName</portlet-name> <!-- This is the name to mention -->
        <display-name xml:lang="EN">My Portlet</display-name>
        <portlet-class>some.package.name.MyPortlet</portlet-class>
        <supports>
          <mime-type>text/html</mime-type>
          <portlet-mode>view</portlet-mode>
        </supports>
        <portlet-info>
          <title>Sample Portlet to showcase GateIn AMD modules</title>
        </portlet-info>
      </portlet>
    </portlet-app>
    

    Then under the gatein-resources.xml file, you would declare your JS modules just the way you did but using the portlet-name:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <gatein-resources
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.gatein.org/xml/ns/gatein_resources_1_5 http://www.gatein.org/xml/ns/gatein_resources_1_5"
        xmlns="http://www.gatein.org/xml/ns/gatein_resources_1_5">
    
      <portlet>
        <name>MyPortletName</name>
        <module>
          <script>
            <name>pagination</name>
            <path>/js/lib/modules/pagination.js</path>
          </script>
        </module>
      </portlet>
    
    </gatein-resources>
    

    When you portlet is injected by the portlet container, the latter will take good care to call all the required modules (JS) you have declared to be dependencies of this portlet usint the gatein-resources.xml (using RequireJS internally), i.e. there is no need to call your script manually usint the <script> element. Just use it.

    EDIT

    In case you need you have common dependencies, when referring to common we mean common between portlets and not JSP files, you can declare a common module and use it under all portlets that need it:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <gatein-resources
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.gatein.org/xml/ns/gatein_resources_1_5 http://www.gatein.org/xml/ns/gatein_resources_1_5"
        xmlns="http://www.gatein.org/xml/ns/gatein_resources_1_5">
    
      <portlet>
        <name>MyPortletName1</name>
        <depends>
          <module>
            <name>pagination</nam>
          </module>
        </depends>
      </portlet>
    
      <portlet>
        <name>MyPortletName2</name>
        <depends>
          <module>
            <name>pagination</nam>
          </module>
        </depends>
      </portlet>
    
      <module>
        <name>pagination</name>
        <script>
          <path>/js/lib/modules/pagination.js</path>
        </script>
      </module>
    
    </gatein-resources>
    

    When injected by the portlet container, inject all JS dependencies into the portlet, thus any JSP page returned within the lifecycle of this portlet will have the JS file injected into it. No need to declare whatever into you JSP because all is done seamlessly at server side thanks to GateIn AMD framework.