自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

利用JSON插件進行數(shù)據(jù)交換格式

開發(fā)
我們想要從Action返回到調(diào)用頁面的數(shù)據(jù)。這個時候配置includeProperties或者excludeProperties攔截器即可。而這2個攔截器的定義都在struts2的json-default包內(nèi)。

JSON(Java Script Object Notation),是一種語言無關(guān)的數(shù)據(jù)交換格式。 JSON插件是Structs 2 的Ajax插件,通過利用JSON插件,開發(fā)者可以很方便,靈活的利用Ajax進行開發(fā)。

Json是一種輕量級的數(shù)據(jù)交換格式,JSon插件提供了一種名為json的Action ResultType 。一旦為Action指定了該結(jié)果處理類型,JSON插件就會自動將Action里的數(shù)據(jù)序列化成JSON格式的數(shù)據(jù),并返回給客戶端物理視圖的JavaScript。簡單的說,JSON插件允許我們在JavaScript中異步的調(diào)用Action。#t#

而且Action不需要指定視圖來顯示Action的信息顯示而是由JSON插件來負責(zé)具體將Action里面具體的信息返回給調(diào)用頁面。Json的數(shù)據(jù)格式可簡單如下形式: person = { name: 'Jim',age: 18,gender: 'man'}。

如果action的屬性很多,我們想要從Action返回到調(diào)用頁面的數(shù)據(jù)。這個時候配置includeProperties或者excludeProperties攔截器即可。而這2個攔截器的定義都在struts2的json-default包內(nèi),所以要使用該攔截器的包都要繼承自json-default。

  1. <struts> 
  2.     <constant name="struts.objectFactory" value="spring"/>       
  3.     <include file="struts-admin.xml"></include>   
  4.     <package name="default" extends="json-default">   
  5.         <action name="person" class="com.person.PersonAction" method="view"> 
  6.         <result type="json"> 
  7.            <param name="includeProperties">             
  8.             person\.name,persoon\.age,person\.gender             
  9.            </param>>             
  10.         </result> 
  11.         </action> 
  12.     </package>         
  13. </struts> 

利用Struts 2的支持的可配置結(jié)果,可以達到過濾器的效果。Action的處理結(jié)果配置支持正則表達式。但是如果返回的對象是一個數(shù)組格式的Json數(shù)據(jù)。比如peson Bean中有對象persion1...person9,而我只要JSON插件,則可以用如下的正則表達式。

  1. <struts> 
  2.     <constant name="struts.objectFactory" value="spring"/>       
  3.     <include file="struts-admin.xml"></include>   
  4.     <package name="default" extends="json-default">   
  5.         <action name="person" class="com.person.PersonAction" method="view"> 
  6.         <result type="json"> 
  7.            <param name="includeProperties">             
  8.             person\.name,persoon\.age,person\.gender             
  9.            </param>>             
  10.         </result> 
  11.         </action> 
  12.     </package>         
  13. </struts> 
  14. 利用Struts 2的支持的可配置結(jié)果,可以達到過濾器的效果。Action的處理結(jié)果配置支持正則表達式。  
  15. 但是如果返回的對象是一個數(shù)組格式的Json數(shù)據(jù)。比如peson Bean中有對象persion1...person9,而我只要person1的json數(shù)據(jù),  
  16. 則可以用如下的正則表達式。  
  17. <struts> 
  18.     <constant name="struts.objectFactory" value="spring"/>       
  19.     <include file="struts-admin.xml"></include>   
  20.     <package name="default" extends="json-default">   
  21.         <action name="person" class="com.person.PersonAction" method="view"> 
  22.         <result type="json"> 
  23.            <param name="includeProperties">             
  24.             person\[\d+\]\.person1  
  25.            </param>>             
  26.         </result> 
  27.         </action> 
  28.     </package>         
  29. </struts> 
  30. excludeProperties攔截器的用法與此類同,如果攔截的僅僅是一個對象,如果攔截掉person Bean的整個對象。  
  31. <struts> 
  32.     <constant name="struts.objectFactory" value="spring"/>       
  33.     <include file="struts-admin.xml"></include>   
  34.     <package name="default" extends="json-default">   
  35.         <action name="person" class="com.person.PersonAction" method="view"> 
  36.         <result type="json"> 
  37.            <param name="excludeProperties">             
  38.             person  
  39.            </param>>             
  40.         </result> 
  41.         </action> 
  42.     </package>         
  43. </struts> 

需要注意的是,如果用JSON插件把返回結(jié)果定為JSON。而JSON的原理是在ACTION中的get方法都會序列化,所以前面是get的方法只要沒指定不序列化,都會執(zhí)行,那么可以在該方法的前面加注解聲明該方法不做序列化。

  1. public String getName()   
  2.  {  
  3.   return this.name;  
  4.  }  
  5. 需要引入 import com.googlecode.jsonplugin.annotations.JSON;  
  6.  
  7. @JSON(serialize=false)  
  8. public User getUser() {  
  9.     return this.User;  
  10. }  
  11.  
  12. @JSON(format="yyyy-MM-dd")  
  13. public Date getStartDate() {  
  14.     return this.startDate;  
責(zé)任編輯:chenqingxiang 來源: 清華大學(xué)出版社
相關(guān)推薦

2009-01-19 09:28:42

JSONJavaScriptJSON結(jié)構(gòu)

2009-06-22 17:57:26

IExtendProv

2010-09-30 10:46:50

J2MEJSONJ2EE

2021-10-15 09:00:02

Python日期轉(zhuǎn)換Python基礎(chǔ)

2023-05-05 19:29:41

2009-11-09 17:17:31

WCF元數(shù)據(jù)交換

2022-02-21 07:51:11

JSON數(shù)據(jù)網(wǎng)絡(luò)

2018-08-31 21:00:39

數(shù)據(jù)交換模型數(shù)據(jù)模型應(yīng)用程序

2023-07-19 19:45:12

EDI人工智能

2010-01-15 10:19:42

數(shù)據(jù)交換技術(shù)

2024-08-07 12:28:06

2019-04-29 10:21:58

JSONXML前端

2019-11-22 08:40:19

ProtobufGo編程語言

2022-05-18 14:17:00

黑客漏洞網(wǎng)絡(luò)攻擊

2010-01-07 17:48:02

JSON結(jié)構(gòu)

2020-06-05 14:29:07

PythonPandas數(shù)據(jù)分析

2012-03-21 09:31:51

ibmdw

2018-05-07 14:50:27

可視化數(shù)據(jù)散點圖

2019-01-03 15:40:38

數(shù)據(jù)庫SQL查詢JSON

2017-12-01 12:09:34

點贊
收藏

51CTO技術(shù)棧公眾號