运用struts2框架和jquery插件对用户登陆进行验证。 前台页面 index.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>My JSP 'index.jsp' starting page</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"> <mce:script type="text/javascript" src="<%=path%><!-- /scripts/jquery-1.4.2.js"> // --></mce:script> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> <mce:script type="text/javascript"><!-- $(document).ready(function(){ $("#submit").click(function(){ var name = $("#name").val(); var password = $("#password").val(); if($.trim(name)=="" || $.trim(password)==""){ alert("please input the password and username again!"); return; } $.post("<%=path%>/ajaxquery.action", {"name":name, "password":password}, function(returnedData, status){ if("success" == status) { //alert(returnedData["name"]); //alert(returnedData["password"]); alert(returnedData["result"]); } },'json'); }); }); // --></mce:script> </head> <body id="body"> username <label> <input name="name" type="text" id="name"> </label> <p>password <label> <input name="password" type="password" id="password"> </label> </p> <input type="button" value="submit" id="submit"> </body> </html> 配置文件struts.xml <?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="jqu" extends="json-default" namespace="/" > <action name="ajaxquery" class="com.action.require.AjaxAction"> <result name="success" type="json"></result> </action> </package> </struts> 后台action package com.action.require; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.google.gson.Gson; import com.opensymphony.xwork2.ActionSupport; public class AjaxAction extends ActionSupport { private String name; private String password; private String result; User myUser ; User yourUser; @Override public String execute() throws Exception { System.out.println(getName()); System.out.println(getPassword()); // myUser = new User(); // myUser.setName(name); // myUser.setPassword(password); // // // Gson gson = new Gson(); // String result = gson.toJson(myUser); // HttpServletResponse response = ServletActionContext.getResponse(); // response.setContentType("application/json; charset=utf-8"); // response.setHeader("Cache-Control", "no-cache"); //取消浏览器缓存 // // PrintWriter out = response.getWriter(); // out.print(result); // // out.flush(); // out.close(); result = name + password; myUser = new User(); myUser.setName(name); myUser.setPassword(password); return "success"; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } user实体bean package com.action.require; public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String password; }