JSON Plugin struts2 的一些设置及使用备忘

  • struts2 的action 的返回值在AJAX调用时,需要返回json格式的数据,struts2中不提供,需要json插件。

 

参考:https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin

 

插件下载:http://code.google.com/p/jsonplugin/(myeclipse中加入了struts2后自带,不需下载)

 

 

json格式如下面所示

{ "otherdata": { "l":["12345","67890"], "name":"999", "startDate":"2011-06-02" }, "resultMsg":"上传文件成功", "success":true }  

 

  • 在extjs 的submit回调函数中可以如下操作返回的json结果

    form.submit({
      url : 'fileupload.action',
      waitMsg: '正在上传...',
      success: function(fp, o) {
         Ext.Msg.alert('上传成功!',o.result.resultMsg);
      },
      failure : function (fp,o){
       Ext.Msg.alert('上传失败!',o.result.resultMsg); 
      }
    });

 

 

  •  JSON annotation 在java中的用法

@JSON(name="filename") //改变属性名称 public String getName() { return name; } @JSON(format="yyyy-MM-dd") //设置日期格式 public Date getStartDate() { return startDate; } @JSON(serialize=false) //设置是否序列化 public List<String> getL() { return l; }

 

 

  •  struts.xml中的json的配置,以下只是基本的配置,详细的请看上面链接中的原版文章

<package name="example" extends="json-default"> <action name="JSONExample" class="com.jfok.JSONExample"> <result type="json"></result> <param name="excludeProperties"> login.password 返回json结果中不包括的属性 </param> <param name="includeProperties"> login.username,userid 返回结果中包括的属性,(可使用正则表达式定义) </param> <param name="root"> otherdata 使用哪个属性作为根属性 </param> <param name="wrapPrefix">/*</param> 在返回json串的开头加上字符 <param name="wrapSuffix">*/</param> 在返回json串的结尾加上字符 以下二行为加一个文件下载的json头的例子 <param name="wrapPrefix"><![CDATA[<html><body><textarea>]]></param> <param name="wrapSuffix"><![CDATA[</textarea></body></html>]]></param> <param name="enableGZIP">true</param> 允许gzip压缩json串 <param name="noCache">true</param> 返回结果不许缓存 </action> </package>

 

 

你可能感兴趣的:(json,struts,MyEclipse,function,正则表达式,action)