無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          處理動態的二級聯動菜單,宗旨,通過ajax異步提交,將通過java業務處理(一些getxx的業務處理方法就省略了)的聯動數據,再通過ajax塞到jsp里

          showSubSpace.jsp:接收數據顯示聯動效果,這個頁面需要初始化自動加載JS  <body onload="showSubSpace()">  *** </body>
          findallSpace.tag:部署在WEB-INF/tags/下,即通過標記文件來獲取板塊信息,初始化頁面信息
          showSubSpace.js:異步提交,用來響應JSP中異步發起請求的,也是ajax-base.js的應用。負責將數據塞到頁面
          ajax-base.js:ajax框架
          ShowSubSpaceServlet.java:獲取后臺數據

          show.jsp
          <%@ page language="java"  pageEncoding="utf-8"%>
          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
          <%@ taglib prefix="mytag" tagdir="/WEB-INF/tags/" %>

          <!-- 刪除二級版塊操作區-->
                          
          <div class="delete_first_conker" id="operate2"  style="display:none">
                          
          <form action="deletesecondconker.do">
                              
          <table class="delete_firstconker_table" cellpadding="0" cellspacing="0">
                              
          <tr>
                                  
          <td class="fronttd"></td>
                                  
          <td class="centertd"></td>
                                  
          <td class="lasttd"></td>
                              
          </tr>
                              
          <tr>
                                  
          <td class="fronttd">一級版塊</td>
                                  
          <td class="centertd">                        
                                  
          <select name="superSpaceId" style="width:150px;" id="firstSpace" onchange="showSubSpace()">
                                      
          <mytag:findallSpace/>
                                      
          <c:forEach items="${first}" var="f">
                                            
          <option value="${f.id }" >${f.name}</option>
                                        
          </c:forEach>
                                  
          </select>
                                  
          </td>
                                  
          <td class="lasttd">*請選擇一級版塊</td>
                              
          </tr>
                              
                              
          <tr>
                                  
          <td class="fronttd">二級版塊</td>
                                  
          <td class="centertd" id="subSpaceSelect">                        
                                  
                                  
          </td>
                                  
          <td class="lasttd">*請選擇二級版塊</td>
                              
          </tr>    
                              
                              
          <tr>
                                  
          <td class="fronttd"></td>
                                  
          <td class="centertd"><input type="image" src="images/button/delete_board.JPG" /></td>
                                  
          <td class="lasttd"></td>
                              
          </tr>
                              
          <tr>
                                  
          <td class="fronttd"></td>
                                  
          <td class="centertd"></td>
                                  
          <td class="lasttd"></td>
                              
          </tr>
                              
          </table>                
                          
          </form>
                      
          </div>

          findallSpace.tag
          <%@ tag body-content="empty" %>
          <%@ tag import="com.handson.bbs.model.*,java.util.*,com.handson.bbs.bo.SpaceBO" %>
          <%    
              SpaceBO spaceBo 
          = SpaceBO.getInstance();
              List
          <Space> first = spaceBo.getFirstSpace();
              request.setAttribute(
          "first",first);
            
          %>

          showSubSpace.js
              function $(id){
                  
          return document.getElementById(id);
              }

                  
              
          function showSubSpace() {
                  
          var superSpaceId = document.getElementById('firstSpace').value;
                  
          var url = 'showsubspace.do?superSpaceId=+ superSpaceId + '&timstamp=+ new Date().getTime();
                  
          //var url = 'showsubspace.do';
                  var ajaxService = new AjaxRequest(url, function(xmlRequest) {
                      
          var resp = xmlRequest.responseText;    
                      $(
          "subSpaceSelect").innerHTML = resp;
                      }
          "GET");
                  
          //encodeURIComponent or encodeURI
                  //ajaxService.content = "superSpaceId=" + superSpaceId;
                  ajaxService.send();
              }

          ajax-base.js
          /**
          * My AJAX service supported base lib
          * Author: Gavin.lee
          * Date: 23/07/2007
          */


          /**
          * AjaxRequest is used to send request based on xml http
          * URL: the url of request
          * callback: the callback function to process response xml http request
          * method: GET/POST, default value is POST, it can be ignored.
          */


          /*
          // for example:
              function checkUser() {
                  var username = document.getElementById("username").value;
                  var url = 'ajax.do?method=checkUser&username=' + username;
                  var ajaxService = new AjaxRequest(url, function(xmlRequest) {
                      var msgDiv = document.getElementById("msgDiv");
                      alert(xmlRequest.responseText);
                      msgDiv.innerHTML = xmlRequest.responseText;
                  });
                  ajaxService.send();
              }
          */


          function $(id){
              
          return document.getElementById(id);
          }

          function AjaxRequest(URL, callback, method) {
              
          this.URL = "";
              
          this.method = "POST";
              
          this.async = true;
              
          this.content = null;
              
          this.callback = function(xmlObj) {}
              
          var xmlHttpRequest;
              
          var objSelf = this;
              
              
          if(URL) {
                  
          this.URL = URL;
              }

              
          if(callback) {
                  
          this.callback = callback;
              }

              
          if(method) {
                  
          this.method = method;
              }

              
              
          if (window.XMLHttpRequest) // Non-IE browser
                  xmlHttpRequest = new XMLHttpRequest();
              }
           else if (window.ActiveXObject) // IE
                  xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
              }

              
              
          if (xmlHttpRequest) {
                  xmlHttpRequest.onreadystatechange 
          = function() {
                      
          if(xmlHttpRequest.readyState == 4//server complete
                          if(xmlHttpRequest.status == 200// OK response
                              objSelf.callback(xmlHttpRequest);
                          }
           else {
                              alert(
          "XMLHttpRequest problem: " + xmlHttpRequest.statusText);
                          }

                      }

                     }
          ;
              }

                  
              
          this.send = function() {
                  
          if(!this.method || !this.URL || !this.async) {
                      
          return;
                  }

                  
                  
          try {
                        xmlHttpRequest.open(
          this.method, this.URL, this.async);
                      
          if(this.method.toUpperCase() == 'POST') {
                          xmlHttpRequest.setRequestHeader(
          "Content-Type""application/x-www-form-urlencoded");
                      }

                      xmlHttpRequest.send(
          this.content);
                   }
           catch (e) {
                      alert(
          "XMLHttpRequest problem: " + e);
                    }

              }
          ;
          }


          ShowSubSpaceServlet.java web.xml中需要對此進行映射,呵,這個就不啰嗦了。
          package com.handson.bbs.servlet;

          import java.io.IOException;
          import java.util.ArrayList;
          import java.util.List;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import com.handson.bbs.bo.SpaceBO;
          import com.handson.bbs.model.Space;
          public class ShowSubSpaceServlet extends BaseServlet 
          {
              public 
          void doGet(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException 
          {

                  response.setContentType(
          "text/html");
                  String superSpaceId 
          = request.getParameter("superSpaceId");
                  List
          <Space> subSpaceList = new ArrayList<Space>();
                  SpaceBO spaceBo 
          = SpaceBO.getInstance();
                  subSpaceList 
          = spaceBo.findSpaceBySpuerId(Integer.parseInt(superSpaceId));
                  request.setAttribute(
          "subSpaceList", subSpaceList);
                  
          this.forward(request, response, "/showSubSpace.jsp");
              }

              public 
          void doPost(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException 
          {
                  
          this.doGet(request, response);
              }

          }


          評論

          # re: 二級聯動菜單(jsp + jstl + tag + ajax + servlet + 業務處理)[未登錄]  回復  更多評論   

          2013-05-13 15:45 by 嘿嘿
          分公司登記費給大家
          主站蜘蛛池模板: 凤山市| 故城县| 长宁区| 宝兴县| 弋阳县| 探索| 章丘市| 平昌县| 前郭尔| 苏州市| 吴旗县| 克东县| 阜阳市| 灵山县| 上思县| 玛多县| 清涧县| 犍为县| 罗定市| 江陵县| 绩溪县| 阿勒泰市| 德令哈市| 三门峡市| 扶沟县| 松原市| 台北市| 衡阳县| 苍南县| 水城县| 海淀区| 大方县| 玉田县| 漯河市| 日喀则市| 平阳县| 华安县| 新建县| 淮安市| 临邑县| 宝兴县|