JSF自定义组件之二 加入Ajax功能

46 篇文章 0 订阅
29 篇文章 0 订阅
 

上篇中已介绍使用HTML产生一个模拟的下拉框,本篇将为其添加Ajax功能,使其在点击下拉按钮时向一个给定的Servlet发送Request,并将得到的下拉列表信息显示。

本篇将对上篇中的部分文件进行修改,并添加一个Servlet来接收请求并响应列表信息。

HTML页面修改如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5. <title>Drop down list</title> 
  6. <link rel="stylesheet" type="text/css" href="mooncomponent.css"> 
  7. <script type="text/javascript" src="moonscript.js"></script> 
  8. </head> 
  9. <body> 
  10. <input type="text" id="DROPDOWN1_INPUT" name="DROPDOWN1_INPUT" 
  11.     class="dropdown_input"> 
  12. <img src="" class="dropdown_button" id="DROPDOWN1_IMG" 
  13.     name="DROPDOWN1_IMG" onclick="showSelector();"> 
  14. <br> 
  15. <div id="DROPDOWN1_AREA" class="dropdown_area"><select 
  16.     id="DROPDOWN1_SELECTOR" name="DROPDOWN1_SELECTOR" multiple="multiple" 
  17.     class="dropdown_selector" onchange="giveValue();"> 
  18. </select></div> 
  19. </body> 
  20. </html>

JS页面修改如下:

  1. var xmldoc;
  2. // create xmlhttp object to send and receive request from server
  3. function createXMLDoc() {
  4.     if (window.XMLHttpRequest) {
  5.         xmldoc = new XMLHttpRequest();
  6.         if (xmldoc.overrideMimeType) {
  7.             xmldoc.overrideMimeType("text/xml");
  8.         }
  9.     } else if (window.ActiveXObject) {
  10.         try {
  11.             xmldoc = new ActiveXObject("Msxml4.XMLHTTP");
  12.         } catch (e) {
  13.             try {
  14.                 xmldoc = new ActiveXObject("Msxml3.XMLHTTP");
  15.             } catch (e) {
  16.                 try {
  17.                     xmldoc = new ActiveXObject("Msxml2.XMLHTTP");
  18.                 } catch (e) {
  19.                     try {
  20.                         xmldoc = new ActiveXObject("Microsoft.XMLHTTP");
  21.                     } catch (e) {
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.     }
  27. }
  28. // send request via post method
  29. function postRequest(url, msg, eventHandle) {
  30.     if (!xmldoc)
  31.         createXMLDoc();
  32.     xmldoc
  33.             .setRequestHeader("Content-Type",
  34.                     "application/x-www-form-urlencoded");
  35.     xmldoc.onreadystatechange = eventHandle;
  36.     xmldoc.open("POST", url, true);
  37.     xmldoc.send(msg);
  38. }
  39. // send request via get method
  40. function getRequest(url, msg, eventHandle) {
  41.     if (!xmldoc)
  42.         createXMLDoc();
  43.     xmldoc.onreadystatechange = eventHandle;
  44.     xmldoc.open("GET", url, true);
  45.     xmldoc.send(msg);
  46. }
  47. function getResponseString() {
  48.     return xmldoc.responseText;
  49. }
  50. function showSelector() {
  51.     if (document.getElementById("DROPDOWN1_SELECTOR").options.length <= 0) {
  52.         //send request via GET method, the servlet name path is /TEST 
  53.         // send an action named getDropdownList to get the message for dropdownlist
  54.         // set the callback method renderSelector when response complete
  55.         getRequest("../TEST?ACTION=getDropdownList"null, renderSelector);
  56.     } else {
  57.         // not the first time click, show the dropdownlist 
  58.         showDDLLayer();
  59.     }
  60.     
  61.     //cancel the javascript event bubble, end this event
  62.     if (window.event) {
  63.         event.cancelBubble = true;
  64.     } else {
  65.         event.stopPropagation();
  66.     }
  67. }
  68. function renderSelector(str) {
  69.     if (xmldoc.readyState == 4 && xmldoc.status == 200) {
  70.         // response complete
  71.         if (!str) {
  72.             //get the message for the dropdownlist
  73.             str = getResponseString();
  74.         }
  75.         var list = document.getElementById("DROPDOWN1_SELECTOR");
  76.         var items = str.split(",");
  77.         //clear the dropdownlist contains
  78.         list.options.length = 0;
  79.         for ( var i = 0; i < items.length; i++) {
  80.             //add an option for each item
  81.             list.add(new Option(items[i], items[i]));
  82.         }
  83.         showDDLLayer();
  84.     }
  85. }
  86. function showDDLLayer() {
  87.     var ea = document.getElementById("DROPDOWN1_AREA");
  88.     var input = document.getElementById("DROPDOWN1_INPUT");
  89.     ea.style.top = (input.style.top + 40);
  90.     ea.style.left = input.style.left;
  91.     //show drop down list
  92.     ea.style.display = "block";
  93. }
  94. function revertValue() {
  95.     hiddenLayer();
  96. }
  97. function giveValue() {
  98.     document.getElementById("DROPDOWN1_INPUT").value = document
  99.             .getElementById("DROPDOWN1_SELECTOR").options[document
  100.             .getElementById("DROPDOWN1_SELECTOR").options.selectedIndex].value;
  101. }
  102. function hiddenLayer() {
  103.     document.getElementById("DROPDOWN1_AREA").style.display = "none";
  104. }
  105. //register event handler for document  
  106. document.onmouseup = revertValue;

Servlet代码如下:

 

 

  1. package net.moon.jsf.customer.test;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class JSFComponentTestServlet extends HttpServlet {
  9.     // this is just demonstrate the message for the drop down list
  10.     private static final String dropdownList = "ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10";
  11.     @Override
  12.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  13.             throws ServletException, IOException {
  14.         // TODO Auto-generated method stub
  15.         String action = req.getParameter("ACTION");
  16.         if ("getDropdownList".equals(action)) {
  17.             // receive the getDropdownList action, response the drop down list
  18.             // message
  19.             PrintWriter out = resp.getWriter();
  20.             out.print(dropdownList);
  21.             out.flush();
  22.             out.close();
  23.         }
  24.     }
  25.     @Override
  26.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  27.             throws ServletException, IOException {
  28.         // TODO Auto-generated method stub
  29.         super.doPost(req, resp);
  30.     }
  31. }

Servlet注册代码:

  1.   <servlet>
  2.     <servlet-name>JSFComponentTest</servlet-name>
  3.     <servlet-class>net.moon.jsf.customer.test.JSFComponentTestServlet</servlet-class>
  4.   </servlet>
  5.   <servlet-mapping>
  6.     <servlet-name>JSFComponentTest</servlet-name>
  7.     <url-pattern>/TEST</url-pattern>
  8.   </servlet-mapping>

 从下篇开始,将开始将该页面逐步转换为JSF组件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值