項目模組擴充團隊不同,採用架構不同,前台有prototype轉為jQuery,下示記錄ajax調用區分
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><script type="text/javascript" src="./js/prototype/prototype.js"></script><script type="text/javascript" src="./js/jquery/jquery-1.6.2.min.js"></script><script type="text/javascript" language="javaScript">function callServletByPrototypeAjax(){new Ajax.Request("test1.prototype",{parameters: "para1=val1¶2=val2",method:'post',onComplete:function(transport){alert("er");var valReturned = transport.responseText;alert("value returned from server port is " + valReturned);}});}function callServletByJQueryAjax(){jQuery.ajax({type:"post",url:"test1.jQuery?para1=value1",dataType:"text",success:function(data){alert("value returned from sever is : " + data );}});}</script></head><body><!--<form action=".do" method="get">--><form name="formRequest" action=".do" method="post"><input type="text" name="form1Para1" value="value1"/><input type="submit" value="submit to show page"/></form><form name="prototypeAjaxRequest"><input type="button" value="call servlet by prototype Ajax" onclick="callServletByPrototypeAjax();"/></form><form name="JQueryAjax"><input type="button" id="callJQueryAjax" value="call servlet by JQuery Ajax" onclick="callServletByJQueryAjax();"/></form></body></html>
當然,在使用jQuery時,可以使用如下方式給組件註冊相關事件。需要注意prototype和jQuery對$符的衝突。
$(document).ready(function(){$("#callJQueryAjax").click(function(){jQuery.ajax({type:"post",url:"test1.jQuery?para1=value1",dataType:"text",success:function(data){alert("value returned from sever is : " + data );}});});};
上述Ajax申請資源為普通servlet,web.xml簡要配置如下,亦可在servlet3.0下使用註解方式。
<servlet> <servlet-name>test1</servlet-name> <servlet-class>com.test.Test1OfPrototype</servlet-class> </servlet> <servlet-mapping> <servlet-name>test1</servlet-name> <url-pattern>/test1.prototype</url-pattern> </servlet-mapping> <servlet> <servlet-name>test1JQuery</servlet-name> <servlet-class>com.test.Test1OfJQuery</servlet-class> </servlet> <servlet-mapping> <servlet-name>test1JQuery</servlet-name> <url-pattern>/test1.jQuery</url-pattern> </servlet-mapping>