struts + ajax 小弟初学就出问题了
说好不能打脸
Java领域优质创作者
博客专家认证 2006-11-06 03:05:41 这是我的代码片断,有注释的,好读得很
testAjax_text.jsp=================================================
<script language=javascript>
//测试代码,现在只进行IE下的初始化,在以后的正式开发中,再进行通用性质的对象初始化
http_request = new ActiveXObject("Microsoft.XMLHTTP");
function doOnclick(){
//设置变更时的监听
http_request.onreadystatechange = onTextResponse;
//发送请求
var onClcikNumber = document.getElementById("onClcikNumber").value;
window.alert("/testAjax/testAjax_textAction.do?number="+onClcikNumber);
http_request.open("GET","testAjax_textAction.do?number="+onClcikNumber,true);
http_request.send(null);
}
//该方法用户当服务器正常响应并返回结果时,处理服务器的响应信息
function onTextResponse(){
if(http_request.readyState == 4 && http_request.status == 200){
//信息处理状态正确,开始处理信息
var textValue = http_request.responseText;
window.alert(textValue);
document.getElementById("mydiv").innerHTML = "您现在是第" + textValue + "次点击按钮";
}
}
</script>
<input type=button onclick="doOnclick()" value="点击此处">
<input type=hidden id="onClcikNumber" value="0">
<div id="mydiv">
</div>
//=====================================================配置文件的内容
<!-- test Ajax -->
<action path="/testAjax/testAjax_textAction" type="org.silverfly.struts.testajax.action.TestAjax_textAction" scope="request">
<forward name="suss" path="/testAjax/testAjax_text.jsp"></forward>
</action>
//action中的内容
/**
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
//开始接受信息
String number = request.getParameter("number") == null?"0":request.getParamete("number");
//开始回发响应
PrintWriter out = response.getWriter();
out.print((Integer.parseInt(number)) + 1);
out.close();
return actionMapping.findForward("suss");
}
//=============本来我是想返回一个数字的,结果ajax将testAjax_text.jsp中的所有内容全部返回给我了,大哥们,救命啊~~~~