2

我正在研究 struts-rest-pluginindex方法以返回我的Fruit对象​​列表xmljson格式。它真的很好用。

型号类:

class Fruit {

    private String name;
    private double price;

    // constructor

    // getter/setter

    // equals and hash method

}

我想在 xml/json 输出中从我的模型对象中排除一些属性,比如价格。我知道我可以用包装类来包装它,但似乎有很多事情要做。

我试过的:

@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>
...

它不工作。请帮忙。谢谢。

4

1 回答 1

0

您应该使用 XStream 注释从结果中省略必填字段

于 2013-08-27T19:10:35.413 回答