//action类
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class AjaxAction extends ActionSupport {
public String getServerTime(){
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
//获取原始的PrintWriter对象,以便输出响应结果,而不用跳转到某个试图
HttpServletResponse response = ServletActionContext.getResponse();
//设置字符集
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//直接输入响应的内容
out.println(sd.format(new Date()));
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;//不需要跳转某个视图 因为上面已经有了直接输出的响应结果
}
public String ajaxLogin(){
try {
String responseText="";
//读取请求的参数
HttpServletRequest request = ServletActionContext.getRequest();
String loginname= request.getParameter("loginname");
String loginpwd= request.getParameter("loginpwd");
//进行登录验证
if(loginname.equalsIgnoreCase("1")){
responseText="该用户名已经被注册!!";
}else{
responseText="该用户名字还可以注册!!";
}
//获取原始的PrintWriter对象,以便输出响应结果,而不用跳转到某个试图
HttpServletResponse response = ServletActionContext.getResponse();
//设置字符集
response.setContentType("text/plain");//设置输出为文字流
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//直接输出响应的内容
out.println(responseText);
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;//不需要跳转某个视图 因为上面已经有了直接输出的响应结果
//http://www. dukai168.cn/blog/157.html
}
}
//ajax 请求服务器返回结果
<script language="javascript">
//定义一个变量用于存放XMLHttpRequest对象
var xmlHttp;
//改函数用于创建一个XMLHttpRequest对象
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
//这是一个启动AJAX异步通信的方法
function getServerTime(){
alert("我走过了时间方法");
var now = new Date();//获取系统当前的时间
//创建一个XMLHttpRequest对象
createXMLHttpRequest();
//将状态触发器绑定到一个函数
xmlHttp.onreadystatechange= processServerTime;
//通过GET方法向指定的URL建立服务器的调用,加个临时的参数,以便表示一个全新的请求
var url="ajax_getServerTime.action?tmp="+now.getTime();//等写完回来看这一句话
xmlHttp.open("GET",url,true);
//发送请求
xmlHttp.send(null);
}
//这是一个用来处理状态改变的函数
function processServerTime(){
//定义一个变量用于存放从服务器返回的响应结果
var responseContext;
if(xmlHttp.readyState==4){
//如果响应成功
if(xmlHttp.status==200){
//取出服务器的响应内容
responseContext=xmlHttp.responseText;
document.getElementByIdx_x("servertime").innerHTML=responseContext;
}
}
}
//这是一个启动AJAX异步通信的方法
function ajaxLogin(){
var ln = document.getElementByIdx_x("loginname").value;
var lp = document.getElementByIdx_x("loginpwd").value;
//创建一个XMLHttpRequest对象
createXMLHttpRequest();
//将状态绑定到一个函数
xmlHttp.onreadystatechange=processAjaxLogin;
//通过GET方法向指定的URL建立服务器的调用
var url="ajax_ajaxLogin.action?loginname="+ln;
xmlHttp.open("GET",url,true);
//发送请求
xmlHttp.send(null);
}
//这是一个用来处理状态改变的函数
function processAjaxLogin(){
//定义一个变量用于存放 从服务器返回的响应结果
var responseContext="";
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
responseContext = xmlHttp.responseText;
alert(responseContext);
}
}
}
</script>
//jsp页面
<body>
<span id="servertime"></span>
<input type="button" value="获取服务器端时间" onclick = "getServerTime()"> <BR>
用户名: <input type="text" id="loginname"><BR>
<P></P>密码: <input type="password" id="loginpwd" οnfοcus="ajaxLogin()" > <BR>
</body>
//xml配置
<package name="test1" extends="struts-default">
<action name="ajax_*" class="com.action.AjaxAction" method="{1}">
<result>/show.jsp</result>
</action>
</package>