2013-04-17 26 views
2

我正在使用struts-rest-plugin index方法返回我的Fruit對象的列表,其格式爲xmljson格式。它工作得很好。如何從struts-rest-plugin請求時排除屬性?

class Fruit { 

    private String name; 
    private double price; 

    // constructor 

    // getter/setter 

    // equals and hash method 

} 

我想從XML/JSON輸出,也就是說,價格我的模型對象中排除某些屬性:

Model類。我知道我可以用一個包裝類來包裝它,但它似乎是一堆事情要做。

我曾嘗試:

@Results(@Result(name = "success", type = "redirectAction", params = { 
     "actionName", "fruit"})) 
public class FruitController extends ActionSupport implements 
     ModelDriven<Object> { 

    private int id; 
    private Fruit model = new Fruit(); 
    private List<Fruit> list; 
    private FruitService fruitService = new FruitService(); 

    public void setId(int id) { 
     this.id = id; 
     if (id > 0) { 
      this.model = fruitService.get(this.id); 
     } 
    } 

    public int getId() { 
     return this.id; 
    } 

    public HttpHeaders index() { 
     list = fruitService.getAll(); 
     return new DefaultHttpHeaders("index").disableCaching(); 
    } 

    @Override 
    public Object getModel() { 
     return (list != null ? list : model); 
    } 

    .... 

} 

struts.xml的

... 
<constant name="struts.convention.action.suffix" value="Controller"/> 
<constant name="struts.convention.action.mapAllMatches" value="true"/> 
<constant name="struts.convention.default.parent.package" value="rest-default"/> 
<constant name="struts.convention.package.locators" value="controller" /> 
... 
<interceptor-ref name="params"> 
       <param name="excludeParams">price</param> 
</interceptor-ref> 
... 

它不工作。請幫忙。謝謝。

+0

省略必填字段我沒有設置'excludeParams'上'interceptor'。我在'result'上使用'excludeProperties',你可以參考'http:// struts.apache.org/release/2.2.x/docs/json-plugin.html' – hiway

+0

@HiwayChe我加了這個,並且一直出錯。 「分派器初始化失敗:無法加載配置」。你能給我一個struts.xml的例子嗎?我很困擾。 – Drogba

+0

'<動作名稱= 「消息_ *」 類= 「com.xxx.action.MessageAction」 \t \t \t方法= 「{1}」> \t \t \t <結果名稱= 「search_ajax」 類型= 「JSON」> \t \t \t \t 結果\ [\ d + \] \。seriesSet \t \t \t \t jsonData \t \t \t','jsonData'是'根'對象是se rialized,它是一個'map','result'是一個具有'list'類型值的鍵,在這裏是'result \ [\ d + \] \。seriesSet'表示排除所有'result'列表中的'seriesSet'屬性。 – hiway

回答

0

你應該使用XStream的註解,從結果