栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

struts2动作类中的Ajax响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

struts2动作类中的Ajax响应

这里,需要对Javascript函数进行校正。当您说时

varxmlHttp
,它在函数内部有作用域,
ajaxEditFunctionCall
而不是在作用域内
showMessage
。另外,
xmlhttp
in
showMessage()
与中的
xmlHttp
object不同
ajaxEditFunctionCall
。因此,使
varxmlHttp
声明保持全局并进行更正。这是工作代码:

<script type="text/javascript"> var xmlHttp; function ajaxEditFunctionCall(){     var URL = "ajaxcall.action?stateName=State1";     try{         xmlHttp=new XMLHttpRequest();     }catch (e){         try{  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");         }catch (e){  try{      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  }catch (e){      alert("Your browser does not support AJAX!");      return false;  }         }     }     //alert(1);     xmlHttp.onreadystatechange = showMessage;     //alert(2);     xmlHttp.open("GET", URL, true);     //alert(3);     xmlHttp.send(null); } function showMessage() {     //alert("Inside Show Message1");     //alert(xmlHttp.readyState);     if(xmlHttp.readyState==4)     {         alert("Inside Show Message2&ReadyState4");         alert(xmlHttp.responseText);     } }          </script>

Java代码是:

public class CustomerAction extends ActionSupport implements ServletResponseAware {    HttpServletResponse response;    public String ajaxcall() {        System.out.println("Inside AjaxCall");        String errorXml = "This is a Sample to Check";        response.setContentType("text/html");        response.setHeader("Cache-Control", "no-cache");        try { response.getWriter().write(errorXml);        } catch (IOException ioe) { ioe.printStackTrace();        }        return null;    }    public void setServletResponse(HttpServletResponse response) {        this.response = response;    }}

struts.xml是:

<struts>    <constant name="struts.devMode" value="true" />    <package name="default" extends="struts-default">        <action name="ajaxcall"  method="ajaxcall"> <result name="success" >/pages/customer/addCustomer.jsp</result>        </action>    </package></struts>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466638.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号