|
|
因为现在很多的应用都是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按钮就能看到效果了。 |
|