【问题标题】:How to fetch a column from a table(Persistence Database table) into a 'select' list of 'form' in a jsp page如何从表(持久性数据库表)中获取列到 jsp 页面中的“表单”的“选择”列表中
【发布时间】:2012-07-12 14:10:39
【问题描述】:

我想从表(持久性数据库表)中提取一列到 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]

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

【问题讨论】:

标签: hibernate struts2 persistence hql


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 2018-06-16
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多