Struts 1.x和AJAX:Action-Class没有得到请求

时间:2013-10-08 11:26:42

标签: java ajax struts struts-1 no-response

我尝试在Struts 1.x和Ajax之间开发一个简单的通信。 我有一个带有ajax代码的jsp,它应该向Action-Class发送“用户名”和“密码”。

AJAX-Code很好,但是Action-Class没有得到AJAX的响应。
问:你怎么知道Action-Class没有收到请求?
答:因为我在Action-Class中写了一个System.out.println(“...”),它在测试应用程序时从未使用过。

我在没有AJAX的情况下尝试了这个,只是jsp中的一个普通“形式”,它将数据发送到Action-Class。在那里,它没有问题。它获取数据并打印println(“...”)的消息。

我的问题是,为什么Action-Class没有得到AJAX的请求?

以下是一些Code-Snippet:

/// JSP - Ajax-script

<script type="text/javascript"> 
        var resultMsg = "";
        var xmlHttp;

        function doAjaxPost() {

            resultMsg = "";
            var name = document.getElementById("username").value;               
            var pw = document.getElementById("password").value;
            //alert(resultMsg);
            var url = "/Index.do?username=" + name + "&password=" + pw;
            document.getElementById("responseMessage").innerHTML ="";
            document.getElementById("responseMessage").innerHTML += "<br>start doAjaxPost()...\n";
            //alert(resultMsg);
            if (url != "") {
                if (window.XMLHttpRequest) { // Non-IE browsers
                    xmlHttp = new XMLHttpRequest();
                    document.getElementById("responseMessage").innerHTML += "<br>implement new XMLHttpRequest...\n";
                } else if (window.ActiveXObject) { // IE
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                    document.getElementById("responseMessage").innerHTML += "<br>implement new ActiveXObject(' Microsoft.XMLHTTP')...\n";
                }
                try {
                    document.getElementById("responseMessage").innerHTML += "<br>check state...\n";
                    xmlHttp.onreadystatechange = processMessage;
                    document.getElementById("responseMessage").innerHTML += "<br>open connection...\n";
                    xmlHttp.open("GET", url, true);
                    document.getElementById("responseMessage").innerHTML += "<br>send data...\n";
                    xmlHttp.send(null);
                    document.getElementById("responseMessage").innerHTML += "<br>...finished";
                } catch (e) {
                    document.getElementById("responseMessage").innerHTML += "<br>\n/////////" + e + "/////////\n\n";
                    alert(resultMsg);
                }
            }

        }

        function processMessage() {
            document.getElementById("responseMessage").innerHTML += "<br>\tpm: check state...\n";
            document.getElementById("responseMessage").innerHTML += "<br>\tpm: state:\t" + xmlHttp.readyState + "\n";
            //alert(resultMsg);
            if (xmlHttp.readyState == 4) { // Complete
                if (xmlHttp.status == 200) { // OK response
                    document.getElementById("responseMessage").innerHTML += "<br>\tpm: set textfield ...\n";
                    document.getElementById("username").value = "";
                    document.getElementById("responseMessage").innerHTML += "<br>\tpm: set <p>-tag ...\n";
                    document.getElementById("responseMessage").innerHTML = xmlHttp.responseText;
                }else{
                    document.getElementById("responseMessage").innerHTML += "<br>No response: "+xmlHttp.status;
                }
            }else{
                document.getElementById("responseMessage").innerHTML += "<br>State not ready: "+xmlHttp.readyState;
            }
        }
    </script>

///的struts-config.xml

<!-- Form mapping -->   
<form-beans>
    <form-bean name="indexForm" type="com.name.forms.IndexForm"/>
</form-beans>

<!-- Action mapping -->
<action-mappings>
    <action name="indexForm" path="/Index" type="com.name.actions.IndexAction"
        scope="request" parameter="/index.jsp" />

/// IndexAction.java类

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    IndexForm ajaxForm = (IndexForm) form;
    System.out.println("Start IndexAction...");
    // response.setContentType("text/text;charset=utf-8");
    // response.setHeader("cache-control", "no-cache");
    PrintWriter out = response.getWriter();
    // String content = (String) request.getParameter("name");
    out.println("Hello " + ajaxForm.getUsername()+"<br>Ur PW:"+ajaxForm.getPassword());
    out.flush();
    return null;
}

///来自JSP的生成消息(在firefox中测试时)
启动doAjaxPost()...
实现httpReq ...
检查状态......
开放连接......
pm:check state ... /// pm代表jsp中的函数processMessage()
下午:州:1
国家尚未准备好:1
发送数据......
...成品
下午:检查状态......
下午:州:2
国家尚未准备好:2
下午:检查状态......
下午:州:3
国家尚未准备好:3
下午:检查状态......
下午:州:4
无回复:404

正如我之前所说,Eclipse-Console没有打印出来自Action-Class的消息,这让我觉得Action-Class没有得到AJAX的请求。

这是mysteriouse,因为它没有ajax工作,我不能看到问题... 我希望有一个人可以帮助我。 抱歉我的英语不好:S

0 个答案:

没有答案