一尘不染

struts2动作类中的Ajax响应

jsp

我正在尝试使用struts2在jsp(避免表单提交)中实现ajax。我使用ajax代码通过url将请求传递给struts2动作。但是来自struts2的响应不会在jap中填充。其显示“空”值。我使用AJAX在jsp中调用动作的代码如下。

    function ajaxEditFunctionCall(){  
 var xmlHttp;
     var url = "ajaxcall.action?stateName="+frm.stateName.value;  
  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);  
            }  
       }

   Included following code in Action Class:

public String ajaxcall() throws Exception{

     System.out.println("Inside AjaxCall");
     String errorXml = "This is a Sample to Check";

     response.setContentType("text/html"); 
     response.setHeader("Cache-Control", "no-cache"); 
         response.setContentType("text/html");   
     response.getWriter().write(errorXml);

     return null;

}

Struts.xml中包含的代码:

  <action name="ajaxcall" class="com.logic.action.CustomerAction" method="ajaxcall">
       <result name="success" >/pages/customer/addCustomer.jsp</result> 
   </action>

我认为错误是在动作类响应语句和struts.xml中。谁能帮我解决这个问题。提前致谢。


阅读 259

收藏
2020-06-08

共1个答案

一尘不染

这里,需要对JavaScript函数进行校正。当您说时var xmlHttp,它在函数内部有作用域,ajaxEditFunctionCall而不是在作用域内showMessage。另外,xmlhttpin
showMessage()与中的xmlHttpobject不同ajaxEditFunctionCall。因此,使var xmlHttp声明保持全局并进行更正。这是工作代码:

<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" class="com.logic.action.CustomerAction" method="ajaxcall">
            <result name="success" >/pages/customer/addCustomer.jsp</result>
        </action>
    </package>
</struts>
2020-06-08