How to populate selectOneMenu from database table

Hi All,

I am a newbie to JSF. I successfully got selectOneMenu list populated with static values, now I was trying to populate my dropdown list from a database table. Can any one tell me how can I do this? I started using simple tables as follows

tables : dept with dept_code number(4), dept_name varchar2(100)

and emp with emp_code number(4),emp_name varchar2(100),dept_code number(4)

I have already created getter and setters for dept and emp.

I have already created jsp files for dept and emp

Please note that it works when I use static values. This means all of my DB connectivity and hibernate/persistenc setup is fine.

I am using netbeans5.5 with jdk1.6

Please help

Thnx

Alex

[748 byte] By [AlexCoxwella] at [2007-11-26 19:05:23]
# 1

Normally you populate a selectOneMenu after CRUD operations, so your selectOneMenu can grow or shrink dynamically.

Suppose you have a class Member with attributes String name; and int age; plus setter and getter and you want to populate only the name toselectOneMenu

[code]

class Member {

String name;

int age;

//setter and getter for name and age

}

private ArrayList<SelectItem> memberItems = new ArrayList<SelectItem>();

public static void getNameIntoSelectOneMenu() {

List list = "make a query to db to get all Member objets"//hibernate

/*

* these function delete all items in memberItems,otherwise your

*selectOneMenu will have dublicate items

*/

memberItems.removeAll(memberItems);

for (int i = 0; i < list.size(); i++) {

Member m = (Member) list.get(i);

memberItems.add(new SelectItem(m.getName()));

}

}

hamidagaa at 2007-7-9 20:55:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thnx for reply. I will try to test it with my tables.
AlexCoxwella at 2007-7-9 20:55:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

memberItems.removeAll(memberItems) ? Rather use clear() .. Or just (re)instantiate it :)

private List<SelectItem> memberItems;

private void loadSelectItems() {

memberItems = new ArrayList<SelectItem>();

List<Member> list = YourDao.execute(someQuery).getOutput(); // retrieve from table

for (Member m : list) {

memberItems.add(new SelectItem(m.getName()));

}

}

BalusCa at 2007-7-9 20:55:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thnx BalusC for your reply. I copied this code to my EmpBean.java. First of all I did not setup DAO so I was unable to compile. There was error on execute(somequery).getOutput() line. I modified loadSelectItems method in a way to use entitymanager functions as follows

This is my EmpBean code

========================

private List<SelectItem> deptItems;

public void loadSelecttItems() {

deptItems = new ArrayList<SelectItem>();

EntityManagerFactory emf = Persistence.createEntityManagerFactory("learning", new HashMap());

EntityManager em = emf.createEntityManager();

String query = "select dept_code,dept_name from dept";

List<EjbDept> list = em.createQuery(query).getResultList();

for (EjbDept d : list) {

deptItems.add(new SelectItem(d.getDept_Code(),d.getDept_Name()));

}

}

and this is my Emp.jsp code

======================

<h:selectOneMenu id="dept_code" value="#{EmpBean.dept_code}" >

<f:selectItems value="#{EmpBean.deptItems}"/>

</h:selectOneMenu>

and this is my faces-config.xml code

=============================

<managed-bean>

<managed-bean-name>DeptBean</managed-bean-name>

<managed-bean-class>secondproject.DeptBean</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

<managed-bean>

<managed-bean-name>EmpBean</managed-bean-name>

<managed-bean-class>secondproject.EmpBean</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

and I am getting exception

======================

org.apache.jasper.JasperException: javax.servlet.jsp.JspException: Error getting property 'deptItems' from bean of type secondproject.EmpBean

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)

....................

.....................

what is missing?

please help, this is my 2nd day just for this :(

AlexCoxwella at 2007-7-9 20:55:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks Thanks to BalusC. Finally I got my selectOneMenu working :) BalusC u r genious :)Thanks for help.
AlexCoxwella at 2007-7-9 20:55:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...