struts2 + jquery struts2 處理json

來源:互聯網
上載者:User

 

<package name="default" extends="struts-default">
  
  <package name="default" extends="json-default">

.Action中的配置
 
  <action name="testAction" class="com.json.action.TestAction" method="testMethod">

  
  <interceptor-ref name="json" /><!--處理以JSON.前台歸回的json字串,是把action中的所有屬性全數轉化為json字串歸回給瀏覽器了,但是我有時辰需要根據實況歸回部門結果,如何對json的結果進行定製輸出呢?result供給了一些參數替你解決這個問題

  5.1. root參數:從歸回結果中根據ognl運算式掏出你需要輸出的結果
  如:
  action類
  
  public class BaseAction extends ActionSupport implements
  
  {
  
  private Person person = null;
  
  }
  public class BaseAction extends ActionSupport implements {private Person person = null;...}

  bean類
  1
  
  public class Person
  
  {
  
  
  private String name;
  
  
  private int age;
  
  
  }
  public class Person {private String name;private int age;...}
  我們只要輸出person對象的name屬性值,配置如次
  5.1
  <result type="json">

  
  <param name="root">person.name</param>
  
  
  </result>
  <result type="json"><param name="root">person.name</param></result>
  5.2. excludeNullProperties 參數:表示是不是去掉空值, 預設值是false,如果設定為true會自動將為空白的值過濾,只輸出不為空白的值。

 
  <result type="json">
  
  <param name="excludeNullProperties">true</param>
 
  </result>
 
  <result type="json"><param name="excludeNullProperties">true</param></result>
  5.3. ignoreHierarchy 參數:表示是不是紕漏等級,也就是繼承關係,好比:TestAction繼承於BaseAction,那麼TestAction中歸回的json字串預設是不會包含父類BaseAction的屬性值,ignoreHierarchy值預設為true,設定為false後會將父類以及子類的屬性一起歸回。

 
  <result type="json">
 
  <param name="ignoreHierarchy">false</param>
 
  </result>
  <result type="json"><param name="ignoreHierarchy">false</param></result>
  5.4. includeProperties 參數:輸出結果中需要包含的屬性值,這搭Regex以及屬性名稱匹配,可以用“,”分割填充多個Regex。
  如:輸出person的所有屬性
 
  <result type="json">
 
  <param name="includeProperties">person.*, person\.name</param>
 
  </result>
  <result type="json"><param name="includeProperties">person.*, person\.name</param></result>

  5.5. excludeProperties 參數:輸出結果需要剔掉的屬性值,也支援Regex匹配屬性名稱,可以用“,”分割填充多個Regex,大致相同4.4.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.