当前位置: 代码迷 >> JavaScript >> struts2 运用json
  详细解决方案

struts2 运用json

热度:138   发布时间:2012-11-14 10:12:19.0
struts2 使用json
JSON(Java Script Object Notation),是一种语言无关的数据交换格式。 JSON插件是Structs 2 的Ajax插件,通过利用JSON插件,开发者可以很方便,灵活的利用Ajax进行开发。 Json是一种轻量级的数据交换格式,JSon插件提供了一种名为json的Action ResultType 。 一旦为Action指定了该结果处理类型,JSON插件就会自动将Action里的数据序列化成JSON格式的数据, 并返回给客户端物理视图的JavaScript。简单的说,JSON插件允许我们在JavaScript中异步的调用Action,而且Action不需要指定视图来显示Action的信息显示。 而是由JSON插件来负责具体将Action里面具体的信息返回给调用页面。 Json的数据格式可简单如下形式: person = { name: 'Jim',age: 18,gender: 'man'}。 如果action的属性很多,我们想要从Action返回到调用页面的数据。 这个时候配置includeProperties或者excludeProperties拦截器即可。
而这2个拦截器的定义都在struts2的json-default包内,所以要使用该拦截器的包都要继承自json-default。
<struts>
    <constant name="struts.objectFactory" value="spring"/>    
    <include file="struts-admin.xml"></include>
    <package name="default" extends="json-default">
        <action name="person" class="com.person.PersonAction" method="view">
        <result type="json">
           <param name="includeProperties">          
            person\.name,persoon\.age,person\.gender          
           </param>>          
        </result>
        </action>
    </package>      
</struts>
利用Struts 2的支持的可配置结果,可以达到过滤器的效果。Action的处理结果配置支持正则表达式。
但是如果返回的对象是一个数组格式的Json数据。比如peson Bean中有对象persion1...person9,而我只要person1的json数据,
则可以用如下的正则表达式。
<struts>
    <constant name="struts.objectFactory" value="spring"/>    
    <include file="struts-admin.xml"></include>
    <package name="default" extends="json-default">
        <action name="person" class="com.person.PersonAction" method="view">
        <result type="json">
           <param name="includeProperties">          
            person\[\d+\]\.person1
           </param>>          
        </result>
        </action>
    </package>      
</struts>
excludeProperties拦截器的用法与此类同,如果拦截的仅仅是一个对象,如果拦截掉person Bean的整个对象,使用如下配置
<struts>
    <constant name="struts.objectFactory" value="spring"/>    
    <include file="struts-admin.xml"></include>
    <package name="default" extends="json-default">
        <action name="person" class="com.person.PersonAction" method="view">
        <result type="json">
           <param name="excludeProperties">          
            person
           </param>>          
        </result>
        </action>
    </package>      
</struts>

需要注意的是,如果用JSON插件把返回结果定为JSON。而JSON的原理是在ACTION中的get方法都会序列化,
所以前面是get的方法只要没指定不序列化,都会执行。
如果该方法一定要命名为get*(比如实现了什么接口),
那么可以在该方法的前面加注解声明该方法不做序列化。
注解的方式为:@JSON(serialize=false)

除此之外,JSON注释还支持如下几个域:
  serialize:设置是否序列化该属性
  deserialize:设置是否反序列化该属性。
  format:设置用于格式化输出、解析日期表单域的格式。例如"yyyy-MM-dd'T'HH:mm:ss"。

//使用注释语法来改变该属性序列化后的属性名
 @JSON(name="newName")
 public String getName()
 {
  return this.name;
 }
需要引入 import com.googlecode.jsonplugin.annotations.JSON;

@JSON(serialize=false)
public User getUser() {
    return this.User;
}

@JSON(format="yyyy-MM-dd")
public Date getStartDate() {
    return this.startDate;
}

    实际在使用的时候,如果不指定includeProperties或者excludeProperties,同时又没有@JSON(serialize=false),那么会默认返回所有Action中的有get、set方法的属性,通常这样做工程上也没什么问题,而且如果我们在使用extjs的话,那么当发起一个请求如下所示:

      Ext.Ajax.request({
           url : 'json/ViewShowAction_getChildrenDevices.do?viewName='+viewName,
           disableCaching : true,
           method : 'GET',

           。。。

           })

     然后如果我们想要获取服务器端返回的结果,需要在method后面再添加处理函数,如下所示:

        success : function(result, request) {
    // 获取下级路由器列表并显示
    thisDevice.children = [];
    //以下两句是获得该视图的背景图片url,同时对页面背景进行设置,与该函数的其它功能没有交叉,写在这里不够优雅
    var background=Ext.decode(result.responseText).background;
    var deviceList = Ext.decode(result.responseText).deviceList;
    for (i = 0; i < deviceList.length; i++) {
      thisDevice.children[i] = new cernet2.Device(deviceList[i],thisDevice);
      thisDevice.children[i].showInBigMap();

      。。。
    }
    thisDevice.fetchChildrenDone = true;
    thisDevice.showChildrenLinkInMap(viewName);
   },
   failure : function(result, request) {
    //Ext.log('获取路由器时有错误发生,错误内容为:'+ result.responseText);
   }

   实际上extjs是把所有的Action类里面有get set方法的属性都已JSON格式存入了result.responseText中,我们从那里就可以获得所要的数据,比如var background=Ext.decode(result.responseText).background;一句,是得到服务器端的background数据(String类型),而var deviceList = Ext.decode(result.responseText).deviceList;则是从服务器端获得了一个deviceList(List类型)。

  相关解决方案