最近才发现有这么多写AJAX的框架,在不知道DWR之前
我认为prototype是多么的好用。下面贴一个刚做的例子:
jsp页面代码:
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
ISO-8859-1
"
%>
<%
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 ' getaccount.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 " >
<!--
< link rel = " stylesheet " type = " text/css " href = " styles.css " >
-->
< script src = " <%=request.getContextPath() %>/css/prototype.js " ></ script >
< script >
function searchAccount(uid) ... {
var url = 'test.do';
var pars="id="+uid;
var myAjax = new Ajax.Request(
url,
...{
method: 'get',
parameters: pars,
onComplete: showResponse
});
}
function showResponse(originalRequest)
... {
document.getElementById("linkid").innerHTML = originalRequest.responseText;
}
var sign = false ;
function show(uid) ... {
if(sign==false)...{
searchAccount(uid);
sign=true;
}else if(sign==true)...{
document.getElementById("linkid").innerHTML ="######";
sign=false;
}
}
</ script >
</ head >
< body >
< html:form action = " /showaccount " >
< span onclick = " show('2') " id = " linkid " style = " cursor:hand " > ###### </ span >
< html:errors property = " id " />< br />
</ html:form >
</ body >
</ html >
<%
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 ' getaccount.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 " >
<!--
< link rel = " stylesheet " type = " text/css " href = " styles.css " >
-->
< script src = " <%=request.getContextPath() %>/css/prototype.js " ></ script >
< script >
function searchAccount(uid) ... {
var url = 'test.do';
var pars="id="+uid;
var myAjax = new Ajax.Request(
url,
...{
method: 'get',
parameters: pars,
onComplete: showResponse
});
}
function showResponse(originalRequest)
... {
document.getElementById("linkid").innerHTML = originalRequest.responseText;
}
var sign = false ;
function show(uid) ... {
if(sign==false)...{
searchAccount(uid);
sign=true;
}else if(sign==true)...{
document.getElementById("linkid").innerHTML ="######";
sign=false;
}
}
</ script >
</ head >
< body >
< html:form action = " /showaccount " >
< span onclick = " show('2') " id = " linkid " style = " cursor:hand " > ###### </ span >
< html:errors property = " id " />< br />
</ html:form >
</ body >
</ html >
Struts Action中的代码如下:
/**/
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.IOException;
import java.io.PrintWriter;
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.yourcompany.struts.form.TestForm;
/** */ /**
* MyEclipse Struts
* Creation date: 11-13-2007
*
* XDoclet definition:
* @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request" validate="true"
*/
public class TestAction extends Action ... {
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
response.setContentType("text/xml;charset=GBK");
String uid=request.getParameter("id");
PrintWriter out=null;
try ...{
out = response.getWriter();
if("1".equals(uid))...{
out.println("1234.00");
}else if("2".equals(uid))...{
out.println("9876.00");
}
} catch (IOException e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.IOException;
import java.io.PrintWriter;
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.yourcompany.struts.form.TestForm;
/** */ /**
* MyEclipse Struts
* Creation date: 11-13-2007
*
* XDoclet definition:
* @struts.action path="/test" name="testForm" input="/form/test.jsp" scope="request" validate="true"
*/
public class TestAction extends Action ... {
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
response.setContentType("text/xml;charset=GBK");
String uid=request.getParameter("id");
PrintWriter out=null;
try ...{
out = response.getWriter();
if("1".equals(uid))...{
out.println("1234.00");
}else if("2".equals(uid))...{
out.println("9876.00");
}
} catch (IOException e) ...{
// TODO 自动生成 catch 块
e.printStackTrace();
}
return null;
}
}