我尝试在 Struts 1.x 和 Ajax 之间开发一个简单的通信。我有一个带有ajax 代码的jsp,它应该将“用户名”和“密码”发送到Action-Class。
AJAX-Code 很好,但是 Action-Class 没有得到 AJAX 的响应。
问:你怎么知道 Action-Class 没有收到请求?
A:因为我在 Action-Class 中写入了一个 System.out.println("..."),它在测试应用程序时从未使用过。
我在没有 AJAX 的情况下也尝试了这个,只是将数据发送到 Action-Class 的 jsp 中的普通“表单”。就是这样,它可以毫无问题地工作。它获取数据并打印 println("...") 的消息。
我的问题是,为什么 Action-Class 没有得到 AJAX 的请求?
这是一些代码片段:
///JSP - Ajax 脚本
<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: 检查状态... ///pm 代表jsp中的函数processMessage()
pm: state: 1
State not ready: 1
send data...
...完成
pm: check state...
pm: state: 2
State not ready: 2
pm: check state...
下午:状态:3
状态未准备好:3
下午:检查状态...
下午:状态:4
无响应:404
正如我之前所说,Eclipse-Console 没有打印来自 Action-Class 的消息,这使我认为 Action-Class 没有得到 AJAX 的请求。
它很神秘,因为它在没有 ajax 的情况下也能工作,而且我看不到问题所在......我希望有人能帮助我。对不起我的英语不好:S