从下拉选择中选择值时出错

时间:2012-07-13 03:32:00

标签: hibernate jsp struts2 hql

我想从jsp页面中'form'的下拉'select'列表中获取一个选定的值到表单的action类中定义的变量,其中'select'下拉列表本身是从数据库表'Category'的列'name',列表'categoryList',在另一个动作类中定义。

获取所选值(即类别名称)后,我想获取表“类别”的主键“cid”。 Category的列是:id,name

在检索类别的'cid'之后,我想在另一个表'问题'的'cid'栏中填写此cid。

我正在使用struts2和hibernate。

我的专栏是“名称”,表格是“类别” 我已经完成了映射配置和bean类。

生成列表的动作类代码:

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;
    }
}

jsp页面'form'中的代码:

<s:form action="okadddqs" method="post" cssClass="text">
                                <input type="hidden" name="email" value="goods.ramesh@gmail.com"/>
                                <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="#@java.util.LinkedHashMap@{'1':'One',
                                         '2':'Two','3':'Three','4':'Four','5':'Five'}"/>
                                <s:submit value="Update Daily Question"/>
                            </s:form>

我提交新问题类的行动:

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中的映射:

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

有人可以指出可能出现的错误吗?     提前谢谢。

2 个答案:

答案 0 :(得分:1)

正如我们在评论中所讨论的那样,categoryList应为Category类型,并带有getter / setter

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&amp; cname操作

中的NewQuestion个变量(带有getter / setter)

答案 1 :(得分:0)

异常的根本原因来自'categoryList'代码,如异常所示。

有关详细信息,请参阅Find the error in Struts2 dropdown list program?。我很确定你遇到了同样的问题。

如果没有,请发布更多代码,最好是怀疑有问题的代码(categoryList变量及其getter和setter)