struts2+传统ajax怎么实现 求高手帮助 在网上找的没有一个可以用的,
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<script type="text/javascript">
function createXHR() {
//编写创建XHR对象的函数
var myXHR = "";
if (window.ActiveXObject) {
//alert("IE");
myXHR = new ActiveXObject("Microsoft.XMLHTTP");
} else {
//alert("W3C");
myXHR = new XMLHttpRequest();
}
return myXHR;
}
function validate(str1,str2) {
//1.创建XHR对象
var showXHR = createXHR();
alert(showXHR);
//2.设置open方法的相关参数
url = "text_result.action?";
var data = "username=" + str1+"&pwd="+str2 ;
alert(url+data);
showXHR.open("POST", url, true);
//post方式必须要写的一句话
showXHR.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
//3.编写回调函数
showXHR.onreadystatechange = function() {
//测试通讯状态
alert(showXHR.readyState);
if (showXHR.readyState == 4) {
if (showXHR.status == 200) {
//就是接受返回值,处理返回值
var rel = showXHR.responseText;
alert(rel);
getId("ps").innerHTML = rel;
}
}
}
//4.发送请求
showXHR.send(data);
}
function getId(id) {
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="myForm">
帐号:<input type="text" name="username"id="username" /> <br>
密码:<input type="password" name="pwd"id="pwd" /> <br>
<input type="button" name="btn" id="btn" value="确定" onclick="validate(getId('username').value,getId('pwd').value)" style="position: relative;"/><span id="ps" style="color: red"></span></form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="test" extends="struts-default">
<action name="text_result" class="actions.TextResult">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>
package actions;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;
public class TextResult extends ActionSupport {
private String username;
private String pwd;
pu