我正在开发一个使用hibernate框架的web应用程序。我正在使用JSP进行查看。我需要从数据库表中加载特定的数据到组合框中。 在这种情况下,我需要从ROLE表中加载所有角色ID到一个组合框 我的代码如下。加载数据库值到组合框 - JSP,休眠
<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;
}
}
我的问题是,即使组合框是不是在页面显示。任何想法解决这个问题?如果可以,请尽快提出解决方案。谢谢:)
可以格式化源代码。 IDE使用什么? –
使用netbeans IDE的Im –
如果列表大小为0,则检查ArrayList大小,然后不为选择选项设置值。如果