小编典典

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

jsp

我想从jsp页面的“表单”下拉列表中的“选择”列表中获取选定的值,并将其提取到表单的动作类中定义的变量中,其中“选择”下拉列表本身是从列中动态获取的”数据库表“
Category”的“名称”和列表“ categoryList”,该列表在另一个操作类中定义。

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

检索类别的“ cid”后,我想将此ID填写在另一个表“ Question”的“ 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”的获取器和设置器:

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]

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


阅读 345

收藏
2020-06-10

共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());
});

需要声明cidcname您的变量(用的getter / setter)NewQuestion行动

2020-06-10