<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ajax+strutsAction 小例子

    本程序對ajax的一些基礎調用進行了封裝,使用者只要實現具體的事件驅動程序就可以
    了,如本例子的doTest.js, 然后把該文件引進相對應的jsp文件里面,如test.jsp


    test.jsp


    <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
    <script src="ajaxjs/ajaxInit.js" type="text/javascript"></script>
    <script src="ajaxjs/doTest.js" type="text/javascript"></script>
    <html>
    ? <body>
    ? <input name="testText" type="text" value="">
    ? <input type="button" value="test" onclick="doTestRequest(document.getElementById('testText').value,'jgaopass','doTestAfterRequest','responseText');">
    ? </body>
    </html>


    doTest.js


    //測試函數
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
    * 請求
    * doAfterRequestMethodName 請求成功后的要執行的函數名稱
    * responseTypeName ajax異步調用后返回的內容的類型,可以使responseText或者responseXml
    */

    function doTestRequest(userName, userPwd, doAfterRequestMethodName, responseTypeName){?
    ?var param = setQueryString('userName',userName,'userPwd',userPwd);
    ?sendRequest('toStrutsAjaxTest.do',param,doAfterRequestMethodName,responseTypeName);
    }

    //請求成功后的執行內容
    function doTestAfterRequest(responseString){
    ?var teststring = document.getElementById("testText");
    ?teststring.value = responseString;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    ajaxInit.js

    //全局變量
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    var xmlHttp = false;
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////??

    //公共函數
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //創建XMLHttpRequest對象

    function createXMLHttpRequest() {???
    ?if (window.XMLHttpRequest) {//Mozilla 等瀏覽器??? ?
    ??xmlHttp = new XMLHttpRequest();
    ??? ?} else {?? ??
    ??? ??if (window.ActiveXObject) {// IE瀏覽器
    ??? ???try {
    ??? ????xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    ??? ???}
    ??? ???catch (e) {
    ??? ????try {
    ??? ???? ?xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    ??? ????}
    ??????????????? ??catch (e) {
    ??????????????? ??}
    ????????? ??}
    ????? ??}
    ?}
    ? ?if (xmlHttp.overrideMimeType) {//修改MiME類型
    ? ??xmlHttp.overrideMimeType("text/xml");
    ? ?}??
    ? ?if (!xmlHttp) {//創建XMLHttpRequest失敗
    ? ??window.alert("創建XMLHttpRequest失敗");
    ?? ??return false;
    ? ?}
    }

    //向后臺發送請求的參數設置
    function setQueryString(){
    ?var param="";
    ??for(var i=0;i<arguments.length;i++){
    ??param+=arguments[i]+'='+encodeURI(arguments[i+1]);
    ??if(i!=arguments.length-2){
    ???param+="&";
    ???i++;
    ??}else{
    ???break;
    ??}?
    ?}
    ?return param;
    }

    /**
    * 發送請求
    * doAfterRequestMethodName 請求成功后的要執行的函數名稱
    * responseTypeName ajax異步調用后返回的內容的類型,可以使responseText或者responseXml
    */

    function sendRequest(requestUrl,param,doAfterRequestMethodName,responseTypeName){
    ?createXMLHttpRequest();?
    ?xmlHttp.open('POST',requestUrl,true);
    ?xmlHttp.setrequestheader("content-type","application/x-www-form-urlencoded");
    ?xmlHttp.onreadystatechange= function(){regCallBack(doAfterRequestMethodName,responseTypeName);};
    ?xmlHttp.send(param);
    }

    //回調函數
    function regCallBack(doAfterRequestMethodName,responseTypeName){?
    ?if(xmlHttp.readyState == 4){
    ??if(xmlHttp.status == 200){
    ????? ???doAfterRequest(doAfterRequestMethodName,responseTypeName);
    ??}
    ?}
    }

    //請求成功后的執行函數
    function doAfterRequest(doAfterRequestMethodName,responseTypeName){
    ?var responseString = "";
    ?if (responseTypeName!=null){
    ??if (responseTypeName == "responseText"){
    ???responseString = xmlHttp.responseText;
    ??}
    ??if (responseTypeName == "responseXml"){
    ???responseString = xmlHttp.responseXml;
    ??}
    ?}?
    ?if(doAfterRequestMethodName!=null){
    ??eval(doAfterRequestMethodName+"('"+responseString+"')");
    ?}
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    struts-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "
    http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">

    <struts-config>
    ? <data-sources />
    ? <form-beans />
    ? <global-exceptions />
    ? <global-forwards />
    ? <action-mappings >
    ??? <action path="/toStrutsAjaxTest" type="com.jgao.ajax.test.struts.action.ToStrutsAjaxTestAction">
    ?</action>
    ? </action-mappings>
    ? <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
    </struts-config>


    ToStrutsAjaxTestAction.java

    //Created by MyEclipse Struts
    // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl

    package com.jgao.ajax.test.struts.action;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    /**
    ?* MyEclipse Struts
    ?* Creation date: 09-18-2006
    ?*
    ?* XDoclet definition:
    ?* @struts.action validate="true"
    ?*/
    public class ToStrutsAjaxTestAction extends Action {

    ?// --------------------------------------------------------- Instance Variables

    ?// --------------------------------------------------------- Methods

    ?/**
    ? * Method execute
    ? * @param mapping
    ? * @param form
    ? * @param request
    ? * @param response
    ? * @return ActionForward
    ? * @throws IOException
    ? */
    ?public ActionForward execute(
    ??ActionMapping mapping,
    ??ActionForm form,
    ??HttpServletRequest request,
    ??HttpServletResponse response) throws IOException {
    ??String teststring = request.getParameter("userName");
    ??teststring = teststring + "ok";
    ?? response.getWriter().write(teststring);
    ??return null;
    ?}

    }

    posted on 2006-09-19 10:11 JGAO編程隨筆 閱讀(1002) 評論(1)  編輯  收藏

    評論

    # re: ajax+strutsAction 小例子[未登錄] 2008-08-28 17:18 bobby


    eval(doAfterRequestMethodName+"('"+responseString+"')");無效
      回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2006年9月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产日本亚洲一区二区三区| 久久久久亚洲精品无码系列| 色偷偷亚洲女人天堂观看欧| 亚洲精品视频免费看| 国产成人精品日本亚洲网站| 国产亚洲精品免费视频播放| 中文字幕亚洲乱码熟女一区二区| 在线视频网址免费播放| 日本亚洲视频在线| 无码国产精品一区二区免费模式 | 一级成人生活片免费看| 免费一级毛片女人图片| 日韩电影免费在线观看网址| 亚洲精品无码专区久久同性男| 国产黄在线播放免费观看| 亚洲精品乱码久久久久久| 久久久久久久岛国免费播放| 亚洲精品美女在线观看播放| 一个人免费观看www视频在线| 亚洲熟妇无码AV不卡在线播放| 精品久久久久久久免费加勒比| 免费VA在线观看无码| 亚洲AV无码专区国产乱码4SE| 24小时日本韩国高清免费| 亚洲字幕AV一区二区三区四区| 成人永久免费高清| a级在线免费观看| 亚洲同性男gay网站在线观看| 日韩免费视频播播| jizz18免费视频| 亚洲国产美女福利直播秀一区二区| 一二三四免费观看在线电影 | 黄床大片30分钟免费看| 亚洲香蕉网久久综合影视| 蜜桃成人无码区免费视频网站| 国产亚洲精品成人AA片| 亚洲国产人成中文幕一级二级| 久久一区二区三区免费播放| 亚洲精品成a人在线观看夫| 亚洲日韩欧洲无码av夜夜摸| 黄网站色在线视频免费观看|