ADF 頁面使用 JavaScript 例子

一、ADF頁面中嵌入javascript有兩種方式:

1、頁面中直接寫入。舉例:
Java代碼  收藏代碼
  1. <af:resource type="javascript">  
  2.   function sayHello() {  
  3.       alert("Hello, world!")  
  4.   }  
  5.     
  6.   function sayJianlong() {  
  7.       alert("Hello, world!")  
  8.       var greeting = AdfPage.PAGE.findComponentByAbsoluteId("greeting");  
  9.       alert(greeting);  
  10.       greeting.setValue("http://www.ejianlong.com");  
  11.   }  
  12.     
  13.   function sayHtml() {  
  14.       var name = document.getElementById("name").value;  
  15.       alert(name);  
  16.   }  
  17.     
  18. </af:resource>    


2、使用外部鏈接js文件。例如:

Java代碼  收藏代碼
  1. <af:resource type="javascript" source="/mc/js/F00mc004.js"></af:resource>  


二、ADF頁面中對js函數的訪問方式。

Java代碼  收藏代碼
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!-- ADF中使用javascript示例 -->  
  3. <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"  
  4.           xmlns:f="http://java.sun.com/jsf/core"  
  5.           xmlns:h="http://java.sun.com/jsf/html"  
  6.           xmlns:af="http://xmlns.oracle.com/adf/faces/rich">  
  7.   <jsp:directive.page contentType="text/html;charset=UTF-8"/>  
  8.   <f:view>  
  9.     <af:document id="d1" title="my test for javascript\html\adf">  
  10.       
  11.       <!-- ADF Tag內嵌JS代碼方式 -->  
  12.       <af:resource type="javascript">  
  13.         function sayHello() {  
  14.             alert("Hello, world!")  
  15.         }  
  16.           
  17.         function sayJianlong() {  
  18.             alert("Hello, world!")  
  19.             var greeting = AdfPage.PAGE.findComponentByAbsoluteId("greeting");  
  20.             alert(greeting);  
  21.             greeting.setValue("http://www.ejianlong.com");  
  22.         }  
  23.           
  24.         function sayHtml() {  
  25.             var name = document.getElementById("name").value;  
  26.             alert(name);  
  27.         }  
  28.           
  29.       </af:resource>  
  30.         
  31.       <af:resource type="javascript" source="/mc/js/F00mc004.js"></af:resource>  
  32.         
  33.       <!-- 測試ADF JS調用 -->  
  34.       <af:commandButton text="Say Hello" id="button1">  
  35.          <af:clientListener method="sayHello" type="click"/>  
  36.       </af:commandButton>  
  37.         
  38.       <!-- 測試ADF JS調用,通過ADF js組件訪問ADF Component -->  
  39.       <af:commandButton text="Say Jianlong and show it's websit url" id="button12">  
  40.          <af:clientListener method="sayJianlong" type="click"/>  
  41.       </af:commandButton>  
  42.       <af:outputText id="greeting" value="" clientComponent="true"/>  
  43.         
  44.       <!-- 測試ADF Tag與HTML Tag混用,並通過HTML Tag訪問ad:resource標記的JS function -->  
  45.       <af:panelBox text="PanelBox1" id="pb1">  
  46.         <f:facet name="toolbar"/>  
  47.         <form id="form1">  
  48.             <input id="name" type="text" value="please input your name" />  
  49.             <input id="button" type="button" οnclick="sayHtml()" value="sysHTML" />  
  50.         </form>  
  51.       </af:panelBox>  
  52.         
  53.       <!-- 測試ADF Tag與JSF Tag混用,並通過JSF Tag訪問ad:resource標記的JS function -->  
  54.       <af:panelBox text="PanelBox2" id="pb2">  
  55.         <f:facet name="toolbar"/>  
  56.         <h:commandButton id="testJSF" type="button" value="testJSF" οnclick="sayHello()" />  
  57.       </af:panelBox>  
  58.   
  59.       <af:panelFormLayout id="pfl1">  
  60.         <af:form id="f1">  
  61.           <af:commandButton text="access js function from js file" id="cb1">  
  62.             <af:clientListener type="click" method="sayOutFileJS"/>  
  63.           </af:commandButton>  
  64.         </af:form>  
  65.         <f:facet name="footer"/>  
  66.       </af:panelFormLayout>  
  67.   
  68.     </af:document>  
  69.   </f:view>  
  70. </jsp:root>  


F00mc004.js源碼如下:
Java代碼  收藏代碼
  1. function sayOutFileJS() {  
  2.     alert("This is function which is in a js file ");  
  3. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章