将数据库值加载到组合框 - JSP,Hibernate

时间:2015-09-27 16:24:34

标签: java hibernate jsp

我正在使用hibernate框架开发一个Web应用程序。我正在使用JSP进行查看。我需要将数据库表中的特定数据加载到组合框中。 在这种情况下,我需要将所有角色ID从ROLE表加载到组合框 我的代码如下。

 <tr>
                    <th align="left"> Role ID </th>
                    <td>
                        <!-- Combobox to get role id's -->

                        <%
                            try {
                                SessionFactory sf = HibernateUtil.getSessionFactory();
                                Session s = sf.openSession();
                                Transaction tx = s.beginTransaction();

                                Role se = new Role();

                                String hql = "select ROLEID from EAD.ROLE";
                                Query q = s.createQuery(hql);
                                ArrayList li = (ArrayList) q.list();
                                Iterator ie = li.iterator();
                                int size = li.size();
                        %>  

                        <select>   
                            <%
                                while (ie.hasNext()) {
                                    Integer un = se.getRoleid();
                            %>

                            <option value="<%= un%>"><%= un%></option>

                            <%
                                }
                            %>
                        </select>   
                        <%
                            } catch (HibernateException he) {
                                he.printStackTrace();
                    }%>    

                        <!-- end of combobox-->
                    </td>
                </tr> 

Role.java

public class Role implements java.io.Serializable {

    private int roleid;
    private String title;
    private String rolename;

    public Role() {
    }

    public Role(int roleid, String title, String rolename) {
        this.roleid = roleid;
        this.title = title;
        this.rolename = rolename;
    }

    public int getRoleid() {
        return this.roleid;
    }

    public void setRoleid(int roleid) {
        this.roleid = roleid;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRoleName() {
        return this.rolename;
    }

    public void setRoleName(String rolename) {
        this.rolename = rolename;
    }

}

我的问题是甚至组合框没有显示在页面中。有什么想法解决这个问题?如果可以,请尽快提出解决方案。谢谢:))

1 个答案:

答案 0 :(得分:0)

尝试显示选项,

查询:"select r.roleid from Role r";

注意:如果列表没有元素或大小为零,则组合框中不会显示任何选项值

替换<td></td>中的以下代码:

<center>   
    <select>  
        <%
            try {
                SessionFactory sf = HibernateUtil.getSessionFactory();
                Session s = sf.openSession();
                Transaction tx = s.beginTransaction();

                Role se = new Role();

                String hql = "select r.roleid from Role r";
                Query q = s.createQuery(hql);
                ArrayList li = (ArrayList) q.list();
                System.out.println("List Size = " + li.size());
                /*
                    if List Size is 0 then no option values will show
                */
                Iterator ie = li.iterator();
        %>

        <%
            while (ie.hasNext()) {
                Integer un = se.getRoleid();
        %>

        <option value="<%= un%>"><%= un%></option>

        <%
            }
        %>

        <%
            } catch (Exception he) {
                he.printStackTrace();
            }
        %>
    </select>   
</center>