获取有关从下拉选择一个值误差向下选择(Getting error on selecting a va

2019-09-19 10:16发布

我想从下拉在一个jsp页“选择”“形式”的列表转换成在动作类的形式,其中“选择”的定义的变量取一选定值下拉列表本身是从列动态取回'命名‘与列表‘这是一些其他动作类中定义所属分类’类别”数据库表’。

取所选择的值之后(即类别的名称)我想获取主键的表“类别”的“CID”。 类别的栏目有:ID,名称

该类别的检索“CID”后,我想填补这一CID另一个表“问题”的列“CID”。

我使用的struts2和休眠。

我的专栏是“名”和表是“类别”我已经做了映射配置和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>

Getter和豆“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]

可有一个人请指出什么可以b是出错误..? 提前致谢。

Answer 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行动



Answer 2:

你的异常的根本原因是由“所属分类”代码作为例外说来。

看到查找Struts2的下拉列表中的程序错误? 更多细节 。 我敢肯定你是同样的问题。

如果不是请张贴一些更多的代码,优选被怀疑有问题(所属分类变量及其getter和setter)的代码



文章来源: Getting error on selecting a value from a drop down select