从下拉列表中选择一个值时出错选择
我想从一个jsp页面中'form'的下拉'选择'列表中获取一个选定的值到表单的action类中定义的变量中,其中'select'下拉列表本身是从数据库表'Category'的列'name'中动态获取的,列表'categoryList'是在另一个操作类中定义的。从下拉列表中选择一个值时出错选择
获取选定的值(这是类别的名称)后,我想要获取表'分类'的主键'cid'。类别的列是:ID,名称
在检索类别的'cid'后,我想在另一个表'Question'的列'cid'中填写此cid。
我正在使用struts2和hibernate。
我的专栏是'名称'和表'是'类别 我已经做了映射配置和bean类。
我的动作类的代码,其中生成的列表:
<s:form action="okadddqs" method="post" cssClass="text">
<input type="hidden" name="email" value="[email protected]"/>
<s:select label="Select Category :" name="name" list="categoryList" listkey="name" listValue="name"/> //Here the list is generated
<s:textarea label="Your Question " cols="40" rows="5" name="body"/>
<s:textfield name="op1" label="Option 1 :"/>
<s:textfield name="op2" label="Option 2 :"/>
<s:textfield name="op3" label="Option 3 :"/>
<s:textfield name="op4" label="Option 4 :"/>
<s:textfield name="op5" label="Option 5 :"/>
<s:select label="Correct Option :"
name="opc"
list="#@[email protected]{'1':'One',
'2':'Two','3':'Three','4':'Four','5':'Five'}"/>
<s:submit value="Update Daily Question"/>
</s:form>
我的行动来提交新问题类:
在JSP页面中的“形式”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;
}
}
代码
package com.rambo.action;
import beans.Category;
import beans.Question;
import beans.Users;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Session;
/**
*
* @author ROMO
*/
@ManagedBean
@SessionScoped
public class NewQuestion extends ActionSupport {
private String cname;
private List<Category> cl = new ArrayList<Category>();
public List<Category> getCl() {
return cl;
}
public void setCl(List<Category> cl) {
this.cl = cl;
}
@Override
public String execute() throws Exception {
Session session = null;
int c;
//c store the cid of the selected Category name from drop down list.
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
cl = (List<Category>) session.createQuery("from Category c where c.name = '" + getName() + "'");
if (!cl.isEmpty()) {
c = cl.get(0).getCid();
} else {
this.addActionError("Oops. Sorry No Category Available.");
session.close();
return ERROR;
}
u = new Question();
u.setCid(c);
u.setCname(getName());
session.save(u);
session.getTransaction().commit();
} catch (Exception e) {
this.addActionError("Oops. An Error Encountered...! Email address already registered. Try with your new email address.");
session.close();
return ERROR;
}
return SUCCESS;
}
@Override
public void validate() {
if ("".equals(getEmail()) || getEmail() == null) {
this.addActionError("All Fields are Compulsory to input..!");
} else if (getEmail().indexOf("@") < 0 || getEmail().indexOf(",") > 0 || getEmail().indexOf(".") < 0) {
this.addActionError("Please Input a valid email address.");
}
}
}
映射在Category.hbm.xml:
豆 “Category.java” 的<property name="name" type="string">
<column name="NAME" length="20" not-null="true" />
</property>
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]
能有人请指出什么可以b是出错误。 。? 在此先感谢。
的代码,categoryList
应Category
类型的吸气剂/二传手
List<Category> categoryList
然后在你的JSP
<s:select label="Select Category :"
name="cid"
id="cid"
list="categoryList"
listKey="id"
listValue="name"
/>
现在宣布的隐藏字段您表单提交cname
也与cid
<s:hidden name="cname" id="cname"/>
jQuery代码(按您的要求),设置cname
$("#cid").change(function(){
$("#cname").val($(this).find("option:selected").text());
});
需要声明cid
& cname
变量(用的getter/setter)在你的NewQuestion
动作
@Rambo作为根据你编辑的问题:你正在创建'categoryList'的代码在哪里?例外是因为只有部分代码。 – anu 2012-07-13 10:24:43
谢谢..我只是执行你给的代码。几分钟后我会回到问题的状态。是的,我现在编辑我的问题与产生列表的行动类'FindCategory'的代码。 – codeofnode 2012-07-13 10:36:16
对不起,但代码实际上没有工作..提交后,我得到'cname'空白。什么可以成为探头现在..?我已经制作了cname的getter和setter以及cid。 – codeofnode 2012-07-13 13:30:06
您的异常的根本原因来自'categoryList'代码,如异常情况所述。
有关更多详细信息,请参阅Find the error in Struts2 dropdown list program?。我很肯定你是在同一个问题上。
如果不是请张贴一些更多的代码,优选被怀疑正如我们在评论讨论遇到的问题(所属分类变量及其getter和setter)
感谢您的答案,但你给的链接是显示下拉列表没有其选项的问题。 对我来说。下拉列表具有并正确显示其选项。但是它不允许将选定的值发送回另一个操作类opon按提交按钮。 你可以指出现在的问题..? – codeofnode 2012-07-13 07:24:08
请解决我的另一个问题在这里http://stackoverflow.com/questions/11480674/how-to-automatically-copy-up-the-value-of-selected-option-of-dropdown-list-into – codeofnode 2012-07-14 05:57:55
你为什么要从你的选择列表中提交'cname'为什么不直接提交'cid'。这将减少您的操作类中不必要的代码。还提到的错误是你的方式youu popu;在列表中不是在这个类别 – anu 2012-07-13 06:29:18
其实我需要同时提交'cname'和相应的 – codeofnode 2012-07-13 07:29:55
但是你在你的班级只使用'cname'来取回相应的'cid' from同一个表'Category'.So,为什么不直接从jsp那里做并保存1个查询。你是否将'canme'用于其他目的? – anu 2012-07-13 07:37:43