如何从表(持久性数据库表)中获取列到jsp页面中“表单”的“选择”列表

时间:2012-07-12 14:10:40

标签: hibernate struts2 persistence hql

我想从一个表(Persistence Database表)中获取一个列到jsp页面中'form'的'select'列表。 我正在使用struts2和hibernate。

我的专栏是“名称”,表格是“类别” 我已经完成了映射配置和bean类。

jsp页面'form'中的代码:

<s:select label="Select Category :" name="cname" list="categoryList" />

我的动作类:

package com.rambo.action;

import beans.Category;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

public class FindCategory extends ActionSupport {

    private List<Category> cl = new ArrayList<Category>();
    private List<String> categoryList = new ArrayList<String>();

    @Override
    public String execute() throws Exception {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            this.cl = (List<Category>) session.createQuery("from Category").list();
            if (this.cl.isEmpty()) {
                this.addActionError("Sorry.. No category Available. Try again Later.!");
                return ERROR;
            }
            for (int i = cl.size()-1; i >= 0; i--) {
                categoryList.add(cl.get(i).getName());
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            this.addActionError("Oops. An Error Encountered...!");
            return ERROR;
        }
        return SUCCESS;
    }
}

Category.hbm.xml中的映射:

<property name="name" type="string">
            <column name="NAME" length="20" not-null="true" />
        </property>

bean“Category.java”的getter和setter:

public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

我的glassfish服务器显示错误:

org.apache.jasper.JasperException: tag 'select', field 'list', name 'cname': The requested list key 'categoryList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

root cause tag 'select', field 'list', name 'cname': The requested list key 'categoryList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

有人可以指出可能出现的错误吗? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

categoryList创建公共getter,否则标记无法访问列表。

另外,你在行动中做了太多工作,IMO。