日期:2014-05-16  浏览次数:20339 次

struts2 + jquery struts2 处理json

<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属性值,配置如次
  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 参数:输出结果中需要包含的属性值,这搭正则表达式以及属性名匹配,可以用“,”分割填充多个正则表达式。
  如:输出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 参数:输出结果需要剔掉的属性值,也支持正则表达式匹配属性名,可以用“,”分割填充多个正则表达式,大致相同4.4.