0

我用Hibernate和Struts2搭建了一个小网站

我有一个表调用类别

结构如:

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 而不是它的父级名称!

4

2 回答 2

0

尝试这个:

@Id
@GeneratedValue
@Column(name="idcategory")
private Integer idcategory;
于 2012-11-18T11:18:54.127 回答
0

我用 s:select 的使用 Value 属性解决了我的问题

<s:select name="cat.parentcatid" list="#session.getAllCategoryID" listKey="parentcatid" listValue="categoryname" headerValue="-- Select Category --" headerKey="0" value="parentid"/>
于 2012-11-18T18:54:42.337 回答