六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 77|回复: 0

google sitebricks 学习心得--最简单的ajax应用

[复制链接]

升级  44%

28

主题

28

主题

28

主题

秀才

Rank: 2

积分
116
 楼主| 发表于 2013-1-29 11:35:53 | 显示全部楼层 |阅读模式
因为现在很多的应用都是ajax的应用,所以在这方面做了些尝试。示例只是在上一篇文章中的示例做了些补充。示例中会用到jquery1.4.4。

新增处理请求类Hello.java
@At("/hello")@Service //此标签表示该类是一个服务类,该类处理的请求没有模板文件也就是说没有对应的html文件public class Hello {@Getpublic Reply<Map<String, String>> hello() {System.out.println("hello world");Map<String, String> result = new HashMap<String, String>();result.put("info", "success");return Reply.with(result).as(Json.class);  }}
Reply更多的用法:
请参考:http://code.google.com/p/google-sitebricks/wiki/RestfulWebServices

修改Example.html文件,修改后文件如下:
<!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=UTF-8"><title>Insert title here</title><script type="text/javascript" src="/js/common/jquery-1.4.4.js"></script><script type="text/javascript" src="/js/Example/Example.js"></script></head><body><a href='?appear=${!appear}'>show/hide</a>@ShowIf(appear)<span>${message} from Sitebricks!</span>  <input type="button" value="hello"  /></body></html>

新增Example.js
function hello(){$.ajax({url : "hello",dataType: "json",success : function(data) {alert(data.info);}});}

至此,最简单的ajax应用完成。
运行后,单击hello按钮就能看到效果了。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表