Hibernate 从下拉选择中选择值时出错
我想从jsp页面中“form”的下拉列表“select”中获取一个选定值,并将其放入表单的action类中定义的变量中,其中“select”下拉列表本身是从数据库表“Category”的“name”列中动态获取的,该列的列表“categoryList”是在另一个action类中定义的 获取所选值(即类别名称)后,我希望获取表“Category”的主键“cid”。类别的列包括:id、名称 检索类别的“cid”后,我想将此cid填入另一个表“Question”的“cid”列中 我正在使用struts2和hibernate 我的列是“name”,表是“Category” 我已经完成了映射配置和bean类 生成列表的My code of action类:Hibernate 从下拉选择中选择值时出错,hibernate,jsp,struts2,hql,Hibernate,Jsp,Struts2,Hql,我想从jsp页面中“form”的下拉列表“select”中获取一个选定值,并将其放入表单的action类中定义的变量中,其中“select”下拉列表本身是从数据库表“Category”的“name”列中动态获取的,该列的列表“categoryList”是在另一个action类中定义的 获取所选值(即类别名称)后,我希望获取表“Category”的主键“cid”。类别的列包括:id、名称 检索类别的“cid”后,我想将此cid填入另一个表“Question”的“cid”列中 我正在使用struts
public class FindCategory extends ActionSupport {
private List<Category> categoryList = new ArrayList<Category>();
@Override
public String execute() throws Exception {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
this.categoryList = (List<Category>) session.createQuery("from Category").list();
if (this.categoryList.isEmpty()) {
this.addActionError("Sorry.. No category Available. Try again Later.!");
return ERROR;
}
session.getTransaction().commit();
} catch (Exception e) {
this.addActionError("Oops. An Error Encountered...!");
return ERROR;
}
return SUCCESS;
}
public List<Category> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<Category> categoryList) {
this.categoryList = categoryList;
}
}
我的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”代码 有关更多详细信息,请参阅。我很肯定你也有同样的问题
如果没有,请发布更多代码,最好是怀疑有问题的代码(categoryList变量及其getter和setter)正如我们在评论中所讨论的,
categoryList
应该是带有getter/setter的类型Category
List<Category> categoryList
jQuery code(根据您的要求)设置cname
$("#cid").change(function(){
$("#cname").val($(this).find("option:selected").text());
});
您需要在
NewQuestion
操作中声明cid
和cname
变量(使用getter/setter)为什么要从选择列表中提交“cname”,为什么不直接提交“cid”。它将减少action类中不必要的代码。此外,所提到的错误是在你是popu的方式;如果列表不在这个类中,实际上我需要提交'cname'和相应的内容,但是你在你的类中使用cname
,只是为了从同一个表Category
中获取相应的cid
。那么,你为什么不直接从jsp中这样做并保存一个查询呢。您是否将canme
用于其他目的?实际上,我需要将“cname”和相应的“cid”提交到问题表的新行。(每个“cname”都有唯一的“cid”)。我只有表格上的“cname”列表。我想从列表中提取一个“cname”,然后找到它的“cid”,最后将它们存储在问题表的新行中。我非常感谢任何其他方式或技巧。我使用cname是因为普通用户记住的名称比任何数字id都多。因此我必须在列表选项中显示“cname”。感谢您的回答,但您提供的链接显示了下拉列表没有选项的问题。为了我。下拉列表包含并正确显示其选项。但它不允许按submit按钮将所选值发送回另一个操作类opon。你现在可以指出问题了吗?请根据你编辑的问题在这里@Rambo解决我的另一个问题:你创建分类列表的代码在哪里?例外情况仅是因为该部分代码。谢谢。。我只是在实现你给出的代码。我将在几分钟后回来介绍问题的状态。是的,我现在用生成列表的操作类“FindCategory”的代码编辑了我的问题。很抱歉,代码实际上不起作用。。提交后,我的“cname”为空。现在的问题是什么。。?我已经制作了cname和cid的getter和setter。您确定jquery部分工作正常吗?尝试在jquery代码中放入一条警报语句(或断点),以测试其是否正在设置cname值。您是否能够提交其他(cid)值?异常是否消失?是,正在提交cid,但“cname”仍为空。我正在使用jquery-1.7.2.js。这是版本的问题吗。。?我试图在cname为空时发出警报。但即使是我也没有得到任何改变。我不知道如何编写jquery。你可以发布整个内容标签的代码。谢谢
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
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]
List<Category> categoryList
<s:select label="Select Category :"
name="cid"
id="cid"
list="categoryList"
listKey="id"
listValue="name"
/>
<s:hidden name="cname" id="cname"/>
$("#cid").change(function(){
$("#cname").val($(this).find("option:selected").text());
});