我想从表(持久性数据库表)中获取列到jsp页面中“表单”的“选择”列表中。 我正在使用 struts2 和 hibernate。
我的列是“名称”,表格是“类别” 我已经做了映射配置和bean类。
jsp 页面“表单”中的代码:
<s:select label="Select Category :" name="cname" list="categoryList" />
我的 Action 类:
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]
有人可以指出可能出现的错误吗..? 提前致谢。
最佳答案
为categoryList
创建一个公共(public)getter,否则标签无法访问列表。
而且,在我看来,你在行动中做了太多的工作。
关于hibernate - 如何在jsp页面中从表(持久性数据库表)中获取列到 'select'的 'form'列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453712/