过程 - 批处理 Ajax 请求
如果必须对同一 URL 进行多次 Ajax 请求,则可以使用 JavaScript BatchRequest 对象对其进行批处理,以减少网络流量。创建 BatchRequest 对象时,会向其传递配置对象,以告知其在一个批处理请求中发送请求的最大数量、为处理请求而调用 URL 以及成功或失败的回调方法。然后将请求添加到 BatchRequest 对象中。当达到最大批处理大小或已运行 500 毫秒时,批处理请求将自动异步发送到服务器。同时,您可以继续向 BatchRequest 对象添加请求。
BatchRequest 对象将存在于在其中被调用的页面的生命周期中。当用户离开当前页面时,将会终止 BatchRequest,并删除任何未处理的请求。
以下是如何在 JavaScript 中创建 BatchRequest 的示例。有关完整方法,请参阅 numericJSRenderer.jsfrag
try {
if(!jsca.columns.numericRenderer.batchRequests) {
jsca.columns.numericRenderer.batchRequests = new PTC.jca.
batch.BatchRequest({
url: getBaseHref() + "ptc1/numeric/",
maxBatchSize: 50,
successCallback: function(response) {
var result = PTC.decode(response.responseText);
for (var i=0,length=result.length; i < length; i++) {
$(jsca.columns.numericRenderer.uniqueIdPrefix +
result[i].uniqueId).innerHTML =
result[i].displayValue;
}
},
failureCallback: function(response) {
jsca.columns.numericRenderer.log.error
("ERROR in jsca.columns.numericRenderer()
getting numeric component data for displaying.");
}
});
}
} catch(e) {
// Handle error that could be thrown when creating BatchRequest
}
创建请求后,可调用 addRequest() 方法以向其添加请求,并向其传递发送给 requestHandler.doRequest() 方法的参数。
jsca.columns.numericRenderer.batchRequests.addRequest({
type : data.type,
externalForm : data.rawValue,
isPercent : data.isPct,
isCurrency : data.isCurrency,
uniqueId : uniqueId
});
在此示例中,批处理请求会发送到名为 "NumericController" 的 servlet 引擎控制器类。各个请求可提供有关需要在页面上显示的数字值的信息。控制器将遍历各个请求,调用 Java 类来创建编号的本地化显示值,并返回 JSONObjects 的 JSONArray,其中每项均包含显示值和与页面上 HTML div 标记 id 相对应的唯一 id。成功回调函数会将 div 标记的内容替换为显示值。
如果使用控制器类处理批处理请求,则应使用 Spring @Controller@RequestMapping 注释对其进行注释。以下是 NumericController 类的一部分 (有关完整类,请参阅 NumericController):
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
.
.
.
@Controller
@RequestMapping(value = "/numeric/**")
public class NumericController {
@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT })
protected void convertToString(
@RequestParam("data") final String data,
HttpServletRequest request, HttpServletResponse response) throws IOException {
JSONArray result = new JSONArray();
try {
JSONArray requests = new JSONArray(data);
for (int i = 0; i < requests.length(); i++) {
JSONObject req = requests.getJSONObject(i);
try {
String type = req.getString("type");
String externalForm = req.getString("externalForm");
String isPercent = req.optString("isPercent");
String isCurrency = req.optString("isCurrency");
String uniqueId = req.getString("uniqueId");
String displayValue = NumericFormat.formatValueForDisplay(type,
externalForm,
Boolean.valueOf(isPercent), Boolean.valueOf(isCurrency));
JSONObject singleResponse = new JSONObject();
singleResponse.put("displayValue", displayValue);
singleResponse.put("uniqueId", uniqueId);
result.put(singleResponse);
} catch (Exception e) {
.
.
.
}
}
response.setContentType("application/json; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(result.toJSONString());
} catch (Exception e) {
log.error("Problem displaying and formatting
number in NumericController.", e);
}
这对您有帮助吗?