0

我想从 jsp 页面中的“表单”下拉“选择”列表中获取选定值到表单的操作类中定义的变量中,其中“选择”下拉列表本身是从列中动态获取的数据库表 'Category' 的名称'和列表 '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页面的“形式”中的代码:

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

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

4

2 回答 2

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&cname变量(使用 getter/setter)NewQuestion

于 2012-07-13T10:11:58.733 回答
0

正如异常所说,您的异常的根本原因来自“categoryList”代码。

请参阅在 Struts2 下拉列表程序中查找错误?更多细节 。我很确定您遇到了同样的问题。

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

于 2012-07-13T06:22:07.600 回答