欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ajax局部刷新

程序员文章站 2022-03-04 14:37:27
...


/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.dd.struts.action;

import java.util.List;

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 org.apache.struts.action.DynaActionForm;

import com.dd.util.DBConnection;


public class QueryAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

//struts 的动态ActionForm 在struts_config.xml 里面配置
DynaActionForm queryForm = (DynaActionForm) form;// TODO Auto-generated method stub

String name = (String)queryForm.get("name");

List list = null;
try {
//后台DAO层
list = DBConnection.getInfo(name);
} catch(Exception ex) {
ex.printStackTrace();
}
request.setAttribute("info", list);
return mapping.findForward("result");
}
}



Ajax.js 编写


var xmlHttp ;
function createXDOM() {

if(window.XMLHttpRequest) {

xmlHttp = new XMLHttpRequest;

} else if (window.ActiveXObject) {

xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} else {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}

function getInfoByName() {
createXDOM();

var name = document.getElementsByName("name")[0].value;

var url = "query.do";
var param = "name="+name;
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.onreadyStatechange=function(){
if(xmlHttp.readyState==4) {
//alert(xmlHttp.responseText);
result.innerHTML=xmlHttp.responseText;
} else {
result.innerHTML="请稍等...";
}
}
xmlHttp.send(param);
}



数据输入页面

<%@ page language="java" pageEncoding="GBK"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">

<body>
<table border=1>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
</tr>
<logic:iterate id="i" name="info">
<tr>
<td>${i.cus_id }</td>
<td>${i.cusname }</td>
<td>${i.ctname }</td>
<td>${i.itname }</td>
<td>${i.creditname }</td>
<td>${i.elname }</td>
<td>${i.custel }</td>
<td>${i.cusemail }</td>
<td>${i.cusaddr }</td>
<td>${i.cusremark }</td>
</tr>
</logic:iterate>
</table>
</body>
</html:html>



数据显示页面

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<script type="text/javascript" src="ajax.js"></script>

<html>
<head>
<title>JSP for DynaActionForm form</title>
</head>
<body>
<html:form action="query.do" method="post">
name : <html:text property="name"/><html:errors property="name"/><br/>
<html:button property="btn" value="查询" onclick="getInfoByName()"></html:button>
</html:form>
<hr>
<div id="result"></div>
</body>
</html>

相关标签: ajax局部刷新

上一篇: 局部刷新

下一篇: 局部刷新