首先看下页面strutsAjax.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for strutsAjaxForm form</title>
<SCRIPT language="javascript">
var xml_httprequest = false;
var url;
function CreateXMLHttpRequest(){
if(window.XMLHttpRequest){
xml_httprequest = new XMLHttpRequest();
}else{
if(window.ActiveXObject){
try{
xml_httprequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
xml_httprequest = new ActiveXObject("Microsoft.XMLHTTP");
}
}
}if(!xml_httprequest){
alert("创建XMLHttpRequest对象失败!");
}
return xml_httprequest;
}
function send_xml(url){
CreateXMLHttpRequest();
xml_httprequest.onreadystatechange = process;
xml_httprequest.open("GET",url,true);
xml_httprequest.send(null);
}
function process(){
document.getElementById("td").innerHTML="正在查询数据中...";
if(xml_httprequest.readyState == 4){
if(xml_httprequest.status == 200){
var str = xml_httprequest.responseText;
document.getElementById("td").innerHTML=str;
}else{
alert("页面异常!");
}
}
}
</SCRIPT>
</head>
<body>
<a href="#" onclick="send_xml([color=red]'/StrutsAjaxTest/strutsAjax.do?name=caojianwei');">[/color]sd</a>
<TABLE><TR>
<TD id="td"></TD>
</TR>
</TABLE>
</body>
</html>
再看下Myjsp.jsp
<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<logic:iterate id="v" name="v">
<bean:write name="v" property="name"/>
<bean:write name="v" property="password"/>
</logic:iterate>
下面是Action
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl
package com.StrutsAjaxTest.struts.action;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.StrutsAjaxTest.DBConnect.Query.DBConnectQuery;
import com.StrutsAjaxTest.struts.form.StrutsAjaxForm;
/**
* MyEclipse Struts
* Creation date: 07-17-2006
*
* XDoclet definition:
* @struts.action path="/strutsAjax" name="strutsAjaxForm" input="/form/strutsAjax.jsp" scope="request" validate="true"
*/
public class StrutsAjaxAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DBConnectQuery dbquery = new DBConnectQuery();
//StrutsAjaxForm strutsAjaxForm = (StrutsAjaxForm) form;
//String name = strutsAjaxForm.getName();
//String password = strutsAjaxForm.getPassword();
String name = request.getParameter("name");
Vector v = dbquery.Query(name);
//System.out.println("1"+v);
//System.out.println(name);
request.setAttribute("v",v);
// TODO Auto-generated method stub
return mapping.findForward("success");
}
}
连接数据库的类
package com.StrutsAjaxTest.DBConnect;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
private static Connection con = null;
private DBConnect(){}
public static DBConnect getDBConnect(){
return new DBConnect();
}
public Connection getConnection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:MQIS","sa","sa");
}catch(Exception e){
e.printStackTrace();
System.out.println("创建数据库连接出错!");
}
return con;
}
/*public static void main(String args[]){
System.out.println(DBConnect.getConnection());
}*/
}
操作数据库的类
package com.StrutsAjaxTest.DBConnect.Query;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import com.StrutsAjaxTest.DBConnect.DBConnect;
import com.StrutsAjaxTest.struts.form.StrutsAjaxForm;
public class DBConnectQuery {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public Vector Query(String name){
con = DBConnect.getDBConnect().getConnection();
System.out.println("0");
StrutsAjaxForm saform=null;
int i=1;
try{
Vector v = new Vector();
stmt = con.createStatement();
//System.out.println("1");
rs = stmt.executeQuery("select name,password from admin");
//System.out.println("2");
while(rs.next()){
saform = new StrutsAjaxForm();
String name1 = rs.getString("name");
String password = rs.getString("password");
saform.setName(name1);
saform.setPassword(password);
//System.out.println(name1+"\n"+password);
//System.out.println(i);
i++;
v.add(saform);
}
return v;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
最后是StrutsConfig
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="strutsAjaxForm" type="com.StrutsAjaxTest.struts.form.StrutsAjaxForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="strutsAjaxForm"
input="/form/strutsAjax.jsp"
name="strutsAjaxForm"
path="/strutsAjax"
scope="request"
type="com.StrutsAjaxTest.struts.action.StrutsAjaxAction">
<forward name="success" path="/form/MyJsp.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>