java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity異常的易被忽略的原因
最近在開發Spring+Hibernate+Struts整合開發的時候遇到幾次這個異常,故記之。
除了網上說的:
jsp頁面中各個控制元件的name屬性與Action中模型驅動物件的名字不一致原因之外,還有一個不起眼的原因,不過不注意也可能發生。
我就發生了兩次。就是在Action的程式碼裡面缺少set方法。
例如:
package com.hugehard.campussecond.action;
import java.util.List;
import com.hugehard.campussecond.model.Catagory;
import com.hugehard.campussecond.service.CatagoryManager;
import com.hugehard.campussecond.util.Const;
import com.opensymphony.xwork2.ActionSupport;
public class CatagoryAction extends ActionSupport {
private CatagoryManager catagoryManager;
private Integer id;
private Catagory catagory;
private List<Catagory> catagorys;
private static final long serialVersionUID = 1L;
public void setCatagoryManager(CatagoryManager catagoryManager) {
this.catagoryManager = catagoryManager;
}
public Catagory getCatagory() {
return catagory;
}
public void setCatagory(Catagory catagory) {
this.catagory = catagory;
} //注意這兒,如果忘了set它就會出現上述錯誤。在呼叫add方法的時候
public List<Catagory> getCatagorys() {
return catagorys;
}
public void setCatagorys(List<Catagory> catagorys) {
this.catagorys = catagorys;
}
public CatagoryManager getCatagoryManager() {
return catagoryManager;
}
public String load() {
if (id == null)
catagory = catagoryManager.get(id);
return SUCCESS;
}
public String add(){
catagoryManager.insert(catagory);
return SUCCESS;
}
public String update() {
catagoryManager.update(catagory);
return SUCCESS;
}
public String delete() {
if (id == null)
catagoryManager.delete(id, Const.UNSORTED_ID);
return SUCCESS;
}
public String list() {
catagorys = catagoryManager.list();
return SUCCESS;
}
}
除此之外,還有一個情況,剛剛發現的,也是太粗心了:
<!-- 分類管理 -->
<action name="listCatagorys" class="catagoryAction"
<!-- 注意這兒,看下面我寫了一個addCatagory,成功之後轉向listCatagorys,而我之前在listCatagorys裡寫的是method="add",而正確的是list,如//果是add的話出現的效果就是,資料庫裡面的確也新增進東西了,但是轉到xx/listCatagorys之後卻又來一個null
entity異常。--!>
method="list">
<result name="success">/admin/listCategorys.jsp</result>
</action>
<action name="addCatagory" class="catagoryAction"
method="add">
<result name="success" type="redirect">/admin/listCatagorys</result>
</action>
<action name="editCatagory" class="catagoryAction"
method="load">
<result name="success" type="redirect">/admin/listCatagorys</result>
</action>
<action name="updateCatagory" class="catagoryAction"
method="update">
<result name="success" type="redirect">/admin/listCatagorys</result>
<result name="input" type="redirect">/admin/listCatagorys</result>
</action>