struts2配置json,警告:no result type defined for type 'json'

以下讨论struts2返回类型为json时,指定与不指定json对象的根的问题:

转自:http://www.worlduc.com/blog2012.aspx?bid=1927439
假设在struts.xml中有如下action定义:

<action name="product_group" class="customers.products" method="getGroups"> 
  <result type="json"> <param name="root">groupList</param> </result> 
</action>

在上面的定义中,action的result的type为json,json plugin就可将action中定义为groupList的field自动转换为json格式数据,并返回给前端UI。

但在deploy后,启动tomcat时却报了There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'?的错误,因为struts2找不到json这个result type的定义。解决方法有下面两种:

1.将当前package的extends属性改为"json-default",即让当前package从josn-default继承而不是struts-default继承;

2.但如果当前package确实无法继承"json-default"的话,还可以在当前package中定义result-type,将json给加进去,如下:

<result-types> <result-type name="json" class="org.apache.struts2.json.JSONResult"/> </result-types>

两种方法的原理:

json这个result type是在json-default  (struts2-json-plugin-2.1.8.1.jar\struts-plugin.xml)里面定义的,内容如下(省去了xml和doctype标签):

<struts> 
<package name="json-default" extends="struts-default"> <result-types>
  <result-type name="json" class="org.apache.struts2.json.JSONResult"/></result-types>
  <interceptors> 
    <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/> 
  </interceptors> 
</package>
</struts>

可见,name为"json"的result type是在json-default中定义的,所以,从json-default继承就可以使用json这个result。另外json-default还定义了一个name为"json"的interceptor。

另外,依json-default的定义来看,方法2中还应该再加一个json的interceptor定义才比较合适。

 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.

原文地址:https://www.cnblogs.com/qinxike/p/2818498.html