Ajax的简单实用实例

时间:2023-12-13 14:55:14

我将实现一个简单的Ajax页面无刷新进行用户验证案例:

效果如下图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqQAAACLCAIAAAA4QRw0AAAgAElEQVR4Ae2dCVxN2R/Af+QVUpQ2opqKQqohDbIrkcgSGTsNZYhhxjbWYWbsTAyG7NRfZCc72WIwlmhTKSVt2jd5Lf9z33rfe/flVa/06nc/Pu45v/M7v3PO91a/e9bb4Pbt2z4+Pr6+CwAvJIAEkAASQAJIoA4RCArK5bh434Z1qFHYFCSABJAAEkACSICBADp7BigoQgJIAAkgASRQlwigs69LTxPbggSQABJAAkiAgQA6ewYoKEICtYEAm10sezVigu+fuRNTInuG8jRL2ezS8tIxDQkgAUUjgM5e0Z4Y1leBCWQsm7TuRWKubC3ImNdv0i8773+WTdukZ9c3u3/v7rwpIqVQthzs+9cfp+Uzvk9kLXLy3HLiuYxFk+ISo95l0d4Pop+HRPOqkffgTjjdDi1JtmqiFhJAAvIg0EgeRtAGEkACMhFIfnVn3iI1r5HmdO1nZ07sugd/bR3bjC6FjMvBUR2sUomnVBaRS4s0mfHn7N0mnt/PbhkcML2JNC2hnNVWLauzrsuSI+u8RlkqCeUkpLn22AL7Du67vR0unl5srvtlYy/PHP5+81PLjnocO8VhT8P07d3vnp2hBk2D9/zuNqPMtDW3cVRSmU7PByEb2jdjiZSJESSABKqTADr76qSLtpGABAFTq29dXe3o4qZvgw+GwhBXe126FOLXA9g72oi+AQg1PucXgmoT+nuApnHnGaPbH8vNJ+8HAv9cws6MjgOzdhrCnPyQYXf7P93Pu4+ekX/8yDI3M76YujfTNd+xfXKPST6bDjvsXyRSW7oaPdy0tU1A0EpOEzLcrcd3muaoRiU3VGrU0PEHT74RKsl8pRd6ejo6DCOBGiCAzr4GIGMRSED+BDJinjhNPDNv5VBVmm1jl3GLQOV6wA2BLPbB9VU+kQeu/jXOzkgg5Acajpg3dc32RxlZBQA5cXFgZKTOT4Iubk595p6w62Yok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2LzDp75negvLgn6egLLqeOHQVhsQAtqeU/588KWImpYRQJIIEvEkBn/0VEqFCPCHD74ZVrcNytI4uuw4l1vWjdUY01p04Ra9OmLbh74IDB9On8Hj8cWToWHDb2MhYIeGX2mrbm9TT6CTUidaFZFpFLRkhDJIXlSyrSdgP/16/Lt8acSjrqxr3uVC4vZ2Mjs1lRaSXaLmoAY0igDhJAZ18HHyo2qSoEZHeoYqUY9Jt4vB+1015Mzo32mjqVnjbxj+NELk2Z0UINCGtbfWqgyVgEEqgnBBrWk3ZiM5EAEkACSAAJ1FsC2LOvt48eG85AgIwA141B4Eq0gtP0Cg/+M0D82qJKtP1rVxnLRwLVTgCdfbUjxgIUhQCLxcrMzNTX109ISFCUOjPWs23btqQhpDmMqYzC+tx2RiAoRAJ1jAA6+zr2QLE5lSegra1+6tSpMWPGmJqaVt5KLciZnZ1NGkKaI3td6nPbZaeEmkhAcQmgs1fcZ4c1lzOBhQuHbNhwydvbm00+UavIF+mmt2zZbPHiobI3oj63XXZKqIkEFJcAOnvFfXZYc/kTqJCDlH/xX9VifW77VwWPhSOBmiCAq/FrgjKWgQSQABJAAkjgKxL4P8p5oiU7DmxHAAAAAElFTkSuQmCC" alt="" width="467" height="96" />

实现主要过程:

在UsersAction类中的checkUser方法中接收并验证前台的表单数据,针对不同情况,返回一个状态码code给jsp页面,然后在ajax1.jsp中通过$.post方法接受后台传递过来的状态码

做出不同的响应。

具体代码如下:

1.实体类

package com.bean;

import java.io.Serializable;

public class Users implements Serializable {
private String uname;
private String passwd; public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public Users(String uname, String passwd) {
super();
this.uname = uname;
this.passwd = passwd;
}
public Users() {
super();
} }

2.action类

package com.action;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action; import com.bean.Users; public class UsersAction {
private Users us; public Users getUs() {
return us;
} public void setUs(Users us) {
this.us = us;
}
@Action(value="checkUser")
public String checkUser() {
System.out.println("aaaaaaaaa");
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
try {
PrintWriter out = response.getWriter();
int code = 0;
if (us == null) {
out.print(0);
return null;
} else {
if (us.getUname() == null || us.getUname().trim().equals("")) {
code = 1;
out.print(code);
return null;
} else {
if (us.getPasswd() == null
|| us.getPasswd().trim().equals("")) {
code = 2;
out.print(code);
return null;
} else {
code = 200;
out.print(code);
}
} } out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
}
}

3.ajax1.jsp

<%@ 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>
<head>
<base href="<%=basePath%>"> <title>Ajax练习</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script>
$(function() {
$("#btok").click(function() {
//获取数据
var uname = $("#uname").val();
var passwd = $("#passwd").val();
//将数据组织为json格式
var json = {"us.uname":uname,"us.passwd":passwd};
//进行异步请求
$.post("checkUser.action",json,function(msg){
if(msg == '0') {
alert("用户名和密码错误!");
return;
}
if(msg == '1') {
$("#uerror").html("用户名错误!");
return;
} else {
$("#uerror").html("*");
}
if(msg == '2') {
$("#perror").html("密码错误!");
return;
} else {
$("#perror").html("*");
}
if(msg == '200') {
alert("登陆成功!");
return;
} }); });
});
</script>
</head> <body>
<form name="form1" method="post" action="">
<table width="450" border="1" align="center" cellpadding="1" cellspacing="0">
<tr>
<td colspan="2" align="center" valign="middle" bgcolor="#FFFFCC">用户注册</td>
</tr>
<tr>
<td width="88">账号:</td>
<td width="352"><label for="uname"></label>
<input type="text" name="uname" id="uname">
<span id="uerror" style="color:#F06;">*</span></td>
</tr>
<tr>
<td>密码:</td>
<td><label for="passwd"></label>
<input type="password" name="passwd" id="passwd">
<span id="perror" style="color:#F06;">*</span></td>
</tr>
<tr align="center" valign="middle" bgcolor="#FFFFCC">
<td colspan="2"><input type="button" name="button" id="btok" value="确定">
<input type="reset" name="button2" id="button2" value="重置"></td>
</tr>
</table>
</form>
<br>
</body>
</html>