[英]Load database values to Combo box - 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>
角色.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;
}
}
我的問題是即使組合框未顯示在頁面中。 有解決這個問題的想法嗎? 如果可以,請盡快提出解決方案。 謝謝 :)
嘗試顯示選項,
查詢: "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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.