2

我想从表(持久性数据库表)中提取一列到 jsp 页面中的“表单”的“选择”列表中。我正在使用struts2和hibernate。

我的列是“名称”,表是“类别”我已经进行了映射配置和 bean 类。

jsp页面的“形式”中的代码:

<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]

有人可以指出可能是什么错误..?提前致谢。

4

1 回答 1

3

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

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

于 2012-07-12T15:16:37.507 回答