<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0044)http://www.qqip.cn -->
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>new document</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2180" name=GENERATOR>
<META content="" name=author>
<META content="" name=keywords>
<META content="" name=description>
<STYLE type=text/css>#show_feedBack_message {
BORDER-RIGHT: #f00 1px solid; PADDING-RIGHT: 3px; BORDER-TOP: #f00 1px solid; PADDING-LEFT: 3px; FONT-SIZE: 12px; BACKGROUND: #fc0; PADDING-BOTTOM: 3px; BORDER-LEFT: #f00 1px solid; LINE-HEIGHT: 18px; PADDING-TOP: 3px; BORDER-BOTTOM: #f00 1px solid
}
{
FONT-SIZE: 12px
}
OL LI {
MARGIN: 15px 0px
}
OL LI A {
FONT-SIZE: 14px; COLOR: #00f
}
</STYLE>
</HEAD>
<BODY>
<SCRIPT ......
项目中想用ajax
,于是在网上扒了
n
多资料,犯了
n
多错误,从今天上班到现在一直在处理这个问题,终于还是把它解决了。
当我看到页面的ajax
显示后,我兴奋异常,为了记录自己学习的
ajax
历程,也为了让更多的人少走弯路,特写此一文以记之!
废话不说了,为了更好的理解,我重做了一个小的项目,以加深印象。现在就以这个小项目开始我们的ajax
之旅。
第一步:创建 名为"
ajax" 的 Java Web
项目。
第二步:加入struts2
的
jar
包,这里需要四个包
freemarker.jar ognl.jar struts2-core.jar commons-fileupload.jar commons-io.jar xwork-core-2.1.6.jar
(这个包加上版本号,是因为下文要提到它),这六个包是
struts
必须依赖的
jar
包,什么好说的。
第三步:修改 web.xml
加入
struts
的过滤器,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" ......
绿色通道文件都是走批,一个文件走完流程后,再查询,速度真是慢,要分别从5个表查,并且需要设置关联查询,因此在提交数据后,我希望查询这部分可以用AJAX实现,当数据未返回时,DIV里显示一个加载GIF,等数据返回后再显示提交的数据。而重复查询时也不会再提交数据和刷新,节省了很多资源.
虽然我会用DWR框架,但是我却不会用DWR实现这个功能,因为一直没弄清楚DWR是怎么封装XMLHTTP的,因此还是直接用XMLHTTP的请求状态来做比较直观。
[code]
<script type="text/javascript">
var xmlHttp;
//创建一个xmlHttpRequest对象
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function handleStateChange(){
var img = "<img src='/images/ ......
ajax.dll已经是很老的东西了,但是今天我才用到它,现在把它的使用方法记录下来。
1.在web.config中配置ajax的处理程序。
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
<httpHandlers>
2. 注册处理程序类,ajaxTest是我的处理程序类。
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxTest));
3. 在处理程序类中编写处理方法。
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public void abc(string s)
{
TextBox1.Text = s;
}
4.3.在JS端使用AjaxMethod中的方法,AjaxTest是服务器端的处理程序类,abc是服务器端同名方法,可以接受3个参数,
abc('传给服务器的参数', '回调函数,接收一个参数,是一个response对象', 'context,这个参数还没弄清楚...')
AjaxTest.abc('ffff',function(e){alert(e.value)}, 'abc') ......
来源:haoxuewu - BlogJava
前段时间做项目用到了json,今天我抽时间写了一个struts+ajax+json的例子.
个人感觉ajax+json在很大程度上降低了网络和服务器的IO,是一个很不错的组合!
1:json的lib我用的是json-lib-2.1-jdk15.jar,它可以在
2:struts用的是1.2
3:用到了js第三方prototype.js,主要用它包装的ajax对象,大家也没必要用这个,可以直接在js里用XMLHttpRequest。
以下是例子中所用到的相关文件:
/////////////////////////////////////// toolhxw.js
/**
@hxw 20080602
*/
//回调函数 简单回调函数
function showesay(dataResponse)
{
var data = eval('(' + dataResponse.responseText + ')');
var str='';
str+='<ul>';
str+='<li>'+data.param1; +'</li>';
str+='<li>'+data.param2; +'</li>';
str+='</ul>';
document.getElementById("content").innerHTML=str;
}
//回调函数 复杂回调函数
function showcomplex(dataResponse)
{
var data = eval('(' + dataResponse.responseText + ')');
var str='';
for(var i=0; i<data.js.length; i++)
{
str ......
have been studying parsing JSON from PHP using AJAX to display it in
the client side and jQuery had been a great help to me. Here is a very
simple code in parsing JSON using jQuery that i made.
tablejsondata.php
This file makes the request to a php file and displays the returned data into a table.
<html>
<head>
<title>Parsing JSON from PHP Using jQuery</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript">
</script>
</head>
<body>
<a href="#" id="loaduserdata">User Data</a>
<table id="userdata" border="1">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>City</th>
</thead>
<tbody></tbody>
</table>
<script>
$(document).ready(function(){
});
$("#loaduserdata").clic ......
have been studying parsing JSON from PHP using AJAX to display it in
the client side and jQuery had been a great help to me. Here is a very
simple code in parsing JSON using jQuery that i made.
tablejsondata.php
This file makes the request to a php file and displays the returned data into a table.
<html>
<head>
<title>Parsing JSON from PHP Using jQuery</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript">
</script>
</head>
<body>
<a href="#" id="loaduserdata">User Data</a>
<table id="userdata" border="1">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>City</th>
</thead>
<tbody></tbody>
</table>
<script>
$(document).ready(function(){
});
$("#loaduserdata").clic ......