这是我写的第一个prototype的小例子,哈哈,放上来,有和我一样的刚开始接触prototye的朋友吗?来看看吧。
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Prototype的Ajax.Request和Ajax.Response</title>
- <script src="prototype.js"></script>
- <script language="javascript" type="text/javascript">
- /*Ajax.Request 类体验 */
- function doAjaxRequest(){
- var url = "books.xml";
- var myAjax = new Ajax.Request(url, {
- method: "get",
- onComplete: showResponse
- });
- }
- /*Ajax.Request 类回调函数 */
- function showResponse(request){
- window.alert(request.responseText);
- }
- </script>
- <script language="javascript" type="text/javascript">
- /*Ajax.Update 类体验 */
- function doAjaxUpdate(){
- var url = "index.html";
- var pars = "field=all&show=true";
- var myAjax = new Ajax.Updater("divContent", url, {
- method: "get",
- parameters: pars
- });
- }
- </script>
- </head>
- <body>
- <input type="button" name="ajaxRequest" value="ajaxRequest" onClick="doAjaxRequest()">
- <input type="button" name="ajaxUpdate" value="ajaxUpdate" onClick="doAjaxUpdate()">
- <div id="divContent">
- </div>
- </body>
- </html>
运行后的效果如下图所示:
解读:
使用 Ajax.Request 后所获取的 books.xml 文档内容,onComplete 指定的 showResponse 函数其实是 Ajax 的回调函数,通常在这个回调函数中完成对响应数据的解析和显示。而如果服务器端返回的是已经格式化后的 HTML 代码(这点在 Ruby 中很流行),则可以使用 Ajax.Updater 。使用 Ajax.Updater 将服务器的响应数据填充到指定的 div 中。展示了使用 Ajax.Updater 的执行效果。
所涉及到的文件见附件
==============================================================
现在将它与webwork联系在一起看例子
页面代码
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Prototype的Ajax.Request和Ajax.Response</title>
- <script src="prototype.js">
- </script>
- <script language="javascript" type="text/javascript">
- /*Ajax.Request 类体验 */
- function doAjaxRequest(){
- var url = "../basicinfo/getReceivePersonListAjax.action";
- var myAjax = new Ajax.Request(url, {
- method: "get",
- onComplete: showResponse // 指定请求成功完成时需要执行的方法
- });
- } /*Ajax.Request 类回调函数 */
- function showResponse(request){
- window.alert(request.responseText);
- }
- </script>
- </head>
- <body>
- <input type="button" name="ajaxRequest" value="ajaxRequest" onClick="doAjaxRequest()">
- </body>
- </html>
配置文件代码
- <action name="getReceivePersonListAjax" class="com..basicinfo.action.PersonEmailDetail" method="getReceivePersonListAjax">
- </action>
action代码
- public void getReceivePersonListAjax() throws IOException,
- InterruptedException {
- String xmlString = "";
- HttpServletResponse response = ServletActionContext.getResponse();
- response.setContentType("text/xml;charset=utf-8");//防止出现乱码
- PrintWriter writer = response.getWriter();
- writer.write(xmlString);
- }
这样就将结果成功返回了。
本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/412374,如需转载请自行联系原作者