2011-08-27 70 views
2

我有一個帶有許多其他嵌套對象和對象列表的Java對象。當請求從客戶端到達時,我看到對象只填充到幾個級別。有什麼配置可以設置這是Struts 2嗎?這是我的例子。Struts 2不會從客戶端填充POJO

class MyActionClass extends ActionSupport { 
    private Abc abc; 
    public Abc getAbc() { 
     return abc; 
    } 
    public void setAbc(Abc abc) { 
     this.abc = abc; 
    } 
    public String populate() { 
     MyService myService = new MyService(); 
     abc = myService.getMyAbc(); 
     return SUCCESS; 
    } 
    public String update() { 
     MyService myService = new MyService(); 
     myService.updateAbc(abc); 
     return SUCCESS; 
    } 
} 

class Abc { 
    private List<Def> defList; 
    private Ghi ghi; 
    public void setDefList(List<Def> defList) { 
     this.defList = defList; 
    } 
    public List<Def> getDefList(){ 
     return defList; 
    } 
    public void setGhi(Ghi ghi) { 
     this.ghi = ghi; 
    } 
    public Ghi getGhi() { 
     return ghi; 
    } 
} 

class Def { 
    private String name; 
    private long id; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public long getId() { 
     return id; 
    } 
    public void setId(long id) { 
     this.id = id; 
    } 
} 

class Ghi { 
    private List<Def> defList; 
    private String ghiName; 

    public void setDefList(List<Def> defList) { 
     this.defList = defList; 
    } 
    public List<Def> getDefList() { 
     return defList; 
    } 
    public void setGhiName(String ghiName) { 
     this.ghiName = ghiName; 
    } 
    public String getGhiName() { 
     return ghiName; 
    } 
} 

當我打電話populate方法,當發送到JSP,迭代情況良好,所有的元素。但是,當我嘗試更新時,即在提交表單時,將調用update()方法,但實例變量abc未完全填充。

我已經看到了傳遞的URL,一切似乎都很好。讓我告訴你發生了什麼事。該網址會像(與爲便於理解這裏換行符分裂),

&abc.defList[0].name=alex 
&abc.defList[0].id=1 
&abc.defList[1].name=bobby 
&abc.defList[1].id=2 
&abc.ghi.ghiName=GHINAME 
&abc.ghi.defList[0].name=Jack 
&abc.ghi.defList[0].id=1 
&abc.ghi.defList[1].name=Jill 
&abc.ghi.defList[1].id=2 

在這種情況下,在abcdefListabcghi.ghiName被填充了沒有問題。但abc.ghidefList未填充。這是Struts 2的常見行爲嗎?是否有任何可以被忽略的手段?

+3

你描述的應該可以工作,你可以去任何級別。有沒有什麼可以覆蓋你有一個地方的錯誤。你有支持struts2的日誌嗎?您可以從日誌中獲取大量信息,例如設置了哪些值以及設置了哪些值。 – Quaternion

+0

四元數,謝謝你試圖幫助我。 :) –

回答

1

解決了問題。 Struts 2搖滾。由於我得到的代碼是用於錯誤修復的,不知道里面有什麼,甚至沒有檢查一次。

罪魁禍首是被覆蓋的toString()方法。它沒有在地圖上檢查null,並在其上調用entrySet()方法。這生成了Exception,並阻止Struts填充對象。

爲了更好的理解,Struts在填充時確實調用了toString()方法。如果將來有人面臨這種​​情況,請記住檢查是否覆蓋了toString()以及是否所有內容均已設置。

+0

也許struts調試選項已打開? 在工作區中的某處搜索「struts.devMode」。它可以解釋調用toString()在其映射工作期間的調試信息 –