dojo+json+ajax

前端之家收集整理的这篇文章主要介绍了dojo+json+ajax前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.下载

json-lib-2.4-jdk15.jar,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar

2.程序中引入所需的类文件

import net.sf.json.JSONObject;

3.编写Java文件

public ActionForward test3(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Map<String,Object> map= new HashMap<String,Object>();
List<Category> list=new ArrayList<Category>();
Category c=new Category();
c.setName("jianping");
c.setFid(2);
Category c2=new Category();
c2.setName("yize");
c2.setFid(3);
list.add(c);
list.add(c2);
map.put("total",123);
map.put("list",list);
out.print(JSONObject.fromObject(map).toString());
out.flush();
out.close();
return null;
}

4.页面调用如下:

function sayHello3() {
dojo.xhrGet({
url: "<%=request.getContextPath()%>/test.do?method=test3",//修改成相应的路径
handleAs: "json",0)">此处必为json
load: function(data)
{
var total=data.total;//提取元素total
alert(total);
var items=data.list; //提取元素list for(var i=0;i<items.length;i++) { alert(items[i].name); } },error: function(response) { alert("Error"+response.message); } }); }

原文链接:https://www.f2er.com/dojo/291732.html

猜你在找的Dojo相关文章