0
我使用Hibernate和Struts2的建立一個小網站Struts2的插入和更新小孩和父母同桌
我有一個表調用分類
Struture像:
ID
類別名稱
個
Parentcatid
...
所有我的文件有:
類別POJO的
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = "Category")
public class Category implements Serializable {
private static long serialVersionUID = -3308170321970658110L;
private Integer idcategory;
private String categoryname;
private int homepage;
private String categorynameviet;
private String description;
private String url;
private Category parentidcat;
private List<Category> subcategory ;
/**
* @return the idcategory
*/
@Id
@GeneratedValue
@Column(name="idcategory")
public Integer getIdcategory() {
return idcategory;
}
/**
* @param idcategory the idcategory to set
*/
public void setIdcategory(Integer idcategory) {
this.idcategory = idcategory;
}
/**
* @return the categoryname
*/
@Column(name="categoryname")
public String getCategoryname() {
return categoryname;
}
/**
* @param categoryname the categoryname to set
*/
public void setCategoryname(String categoryname) {
this.categoryname = categoryname;
}
/**
* @return the homepage
*/
@Column(name="homepage")
public int getHomepage() {
return homepage;
}
/**
* @param homepage the homepage to set
*/
public void setHomepage(int homepage) {
this.homepage = homepage;
}
/**
* @return the categorynameviet
*/
@Column(name="categorynameviet")
public String getCategorynameviet() {
return categorynameviet;
}
/**
* @param categorynameviet the categorynameviet to set
*/
public void setCategorynameviet(String categorynameviet) {
this.categorynameviet = categorynameviet;
}
/**
* @return the description
*/
@Column(name="description")
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the url
*/
@Column(name="url")
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the parentidcat
*/
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="parentcatid")
public Category getParentidcat() {
return parentidcat;
}
/**
* @param parentidcat the parentidcat to set
*/
public void setParentidcat(Category parentidcat) {
this.parentidcat = parentidcat;
}
/**
* @return the subcategory
*/
@OneToMany(mappedBy="parentidcat")
public List<Category> getSubcategory() {
return subcategory;
}
/**
* @param subcategory the subcategory to set
*/
public void setSubcategory(List<Category> subcategory) {
this.subcategory = subcategory;
}
}
我的行動
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.dejavu.pirate.dao.CategoryDAO;
import org.dejavu.pirate.model.Category;
public class CategoryAdminAction extends ActionSupport {
private static final long serialVersionUID = -738951644056447324L;
private Category cat;
private static CategoryDAO catDAO = new CategoryDAO();
private List<Category> categoryList;
private boolean ckhomepage;
@Override
public String execute() {
return SUCCESS;
}
public String setUpForInsertOrUpdate() {
getCategoryParent();
if (cat != null && cat.getIdcategory() != null) {
cat = catDAO.findCatById(cat.getIdcategory());
}
return "success";
}
public String InsertOrUpdateCategory() {
int parentId = 0;
if (ckhomepage == true) {
cat.setHomepage(1);
} else {
cat.setHomepage(0);
}
if (cat.getUrl() == null) {
cat.setCategorynameviet(cat.getCategoryname());
}
if (cat.getIdcategory() == null) {
catDAO.addCategory(cat);
} else {
catDAO.updateCategory(cat);
}
return SUCCESS;
}
public void getCategoryParent() {
categoryList = catDAO.getAllCategory();
Map session = ActionContext.getContext().getSession();
session.put("getAllCategoryID", categoryList);
}
@SkipValidation
public String getAllCategory() {
categoryList = catDAO.getAllCategory();
return SUCCESS;
}
public List<Category> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<Category> categoryList) {
this.categoryList = categoryList;
}
/**
* @return the catDAO
*/
public CategoryDAO getCatDAO() {
return catDAO;
}
/**
* @param catDAO the catDAO to set
*/
public void setCatDAO(CategoryDAO catDAO) {
this.catDAO = catDAO;
}
public Category getCat() {
return cat;
}
public void setCat(Category cat) {
this.cat = cat;
}
public boolean isCkhomepage() {
return ckhomepage;
}
public void setCkhomepage(boolean ckhomepage) {
this.ckhomepage = ckhomepage;
}
}
和我的表單代碼,只是一個下拉框:
<s:select name="cat.parentcatid" list="#session.getAllCategoryID" listKey="parentcatid" listValue="categoryname" headerValue="-- Select Category --" headerKey="0" />
我有個問題當更新類別時,它可以加載類別列表,但選擇的是headerValue而不是它的父級名稱!
它不爲我工作@@。 – Shen