prototype+json+struts2创建简单的ajax应用

显示页面
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  3. <html>
  4. <head>
  5. <title>test</title>
  6. <metahttp-equiv="pragma"content="no-cache">
  7. <metahttp-equiv="cache-control"content="no-cache">
  8. <metahttp-equiv="expires"content="0">
  9. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  10. <metahttp-equiv="description"content="Thisismypage">
  11. <!--
  12. <linkrel="stylesheet"type="text/css"href="styles.css">
  13. -->
  14. </head>
  15. <scripttype="text/javascript"src="js/common/prototype.js"></script>
  16. <scriptlanguage="javascript"type="text/javascript">
  17. functiongetJson()
  18. {
  19. varurl='JSONExample.action';
  20. varpars='';
  21. varmyAjax=newAjax.Request(url,{
  22. method:'get',
  23. encoding:'UTF-8',
  24. parameters:pars,
  25. onComplete:function(json){
  26. varJSONobj=eval('('+json.responseText+')');
  27. varhtml="";
  28. varmenuid="";
  29. html+="<li>"+JSONobj.newName+"</li>";
  30. varints=JSONobj.ints;
  31. for(vari=0;i<ints.length;i++){
  32. html+="<li>"+ints[i]+"</li>";
  33. }
  34. varmap=JSONobj.map;
  35. for(arrayinmap){
  36. html+="<li>"+array+":"+map[array]+"</li>";
  37. }
  38. $("item").innerHTML=html;
  39. }
  40. });
  41. }
  42. </script>
  43. <body>
  44. <inputtype="button"name="button"id="button"onclick="getJson()"value="点击测试">
  45. <divid="item"></div>
  46. </body>
  47. </html>

struts.xml配置文件
  1. <packagename="example"extends="json-default">
  2. <actionname="JSONExample"class="jSONExample"method="executeJson">
  3. <resulttype="json"/>
  4. </action>
  5. </package>
这里使用到了json struts2结合的插件,请自己去下载那个jar包


action
  1. packageaction;
  2. importjava.util.HashMap;
  3. importjava.util.Map;
  4. importcom.googlecode.jsonplugin.annotations.JSON;
  5. importcom.opensymphony.xwork2.ActionSupport;
  6. /**
  7. *@author王盛武<ahref="sinlff@163.com"/>
  8. *@dateAug1,200812:10:35PM
  9. */
  10. publicclassJSONExampleextendsActionSupport{
  11. /**
  12. *
  13. */
  14. privatestaticfinallongserialVersionUID=4588482034890372410L;
  15. //封装请求参数的三个属性
  16. //format=例如"yyyy-MM-dd'T'HH:mm:ss"。
  17. privateStringfield1;
  18. privatetransientStringfield2;
  19. privateStringfield3;
  20. //封装处理结果的属性
  21. privateint[]ints={10,20};
  22. @SuppressWarnings("unchecked")
  23. privateMapmap=newHashMap();
  24. privateStringcustomName="custom";
  25. @SuppressWarnings("unchecked")
  26. publicStringexecuteJson(){
  27. map.put("name1","sinlff1");
  28. map.put("name2","sinlff2");
  29. map.put("name3","sinlff3");
  30. returnSUCCESS;
  31. }
  32. //三个请求参数对应的setter和getter方法
  33. @JSON(serialize=false)//format
  34. publicStringgetField1(){
  35. returnfield1;
  36. }
  37. publicvoidsetField1(Stringfield1){
  38. this.field1=field1;
  39. }
  40. @JSON(serialize=false)
  41. publicStringgetField2(){
  42. returnfield2;
  43. }
  44. publicvoidsetField2(Stringfield2){
  45. this.field2=field2;
  46. }
  47. @JSON(serialize=false)//format
  48. publicStringgetField3(){
  49. returnfield3;
  50. }
  51. publicvoidsetField3(Stringfield3){
  52. this.field3=field3;
  53. }
  54. publicvoidsetCustomName(StringcustomName){
  55. this.customName=customName;
  56. }
  57. //封装处理结果的属性的setter和getter方法
  58. publicint[]getInts(){
  59. returnints;
  60. }
  61. publicvoidsetInts(int[]ints){
  62. this.ints=ints;
  63. }
  64. @SuppressWarnings("unchecked")
  65. publicMapgetMap(){
  66. returnmap;
  67. }
  68. @SuppressWarnings("unchecked")
  69. publicvoidsetMap(Mapmap){
  70. this.map=map;
  71. }
  72. //使用注释语法来改变该属性序列化后的属性名
  73. @JSON(name="newName")
  74. publicStringgetCustomName(){
  75. returnthis.customName;
  76. }
  77. }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值