2011-08-03 20 views
5

对不起,我真的不知道如何总结这个问题的标题。所以,标题可能不清楚。在struts'json中只传递一个类的一部分属性

我有一个执行一些业务逻辑的操作类。

在Action类

class ActionClass extends ActionSupport{ 
     private Merchandise merchandise;// I want to transfer it to the client 
     //setter and getter 

} 
在商品类

class Merchandise{ 
    private String name; // I want to transfer it 
    private String price; //I don't want to transfer it 
    private String description;//I don't want to transfer it 
    //setter and getter 
} 

现在,我需要将商品财产ActionClass传送到客户端。

然而,在商品财产,我要同时抑制其他两个属性仅传送财产。

那么如何抑制商品类中其他两个属性(价格和描述)的转移?

+0

你能解释更多的“我要过滤这些属性和转让只是其中的一部分,”如果可能的话 – anu

回答

5

试着这么做:

<!-- Result fragment --> 
<result type="json"> 
    <param name="root">merchandise</param> 
    <param name="excludeProperties">price,description</param> 
</result> 

查看完整的文档,其他选项和示例在http://struts.apache.org/2.2.3/docs/json-plugin.html

+1

真的谢谢,看来我还没有消化文档足以与一些示例,但是。需要更多工作。 :-) – xiaohan2012

0

最简单的方法是创建一个Action类的数据传输对象仅包含您想要的字段发送给客户端,并使您的根对象

0

@nmc答案是正确的另一种方式,你可以尝试像:

<result type="json"> 
    <param name="root">merchandise</param> 
    <param name="includeProperties">name</param> 
</result> 

或者

<result type="json"> 
    <param name="includeProperties"> 
     merchandise.name 
    </param> 
    <param name="root"> 
     #action 
    </param> 
    </result> 
相关问题