I am trying to populate the selectOneMenu tag from the database, however it is not working properly. Please check my code to point out my mistake.
My xhtml code is:
<h:form>
<h:selectOneMenu value="#{dropDownBean.dropDownItem}">
<f:selectItems value="#{dropDownBean.fullName}"/>
</h:selectOneMenu>
</h:form>
My Bean file is: DropDownBean.java
public class DropDownBean
{
private String empID;
private String firstName;
private String lastName;
private String designation;
private String gender;
private String fullName;
private String dropDownItem;
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
private List<EmployeeDutySchedule> dropItems=new ArrayList<EmployeeDutySchedule>();
TaskServices ts=new TaskServices();
public List dropDownList()
{
System.out.println("dropDownList() invoked");
setDropItems(ts.dropDownList());
for(int i=0; i<=(getDropItems().size());i++)
{
setEmpID(getDropItems().get(i).getEmpID());
setFullName(getDropItems().get(i).getFirstName() +" "+ getDropItems().get(i).getLastName());
setDesignation(getDropItems().get(i).getDesignation());
System.out.println(getEmpID()+" "+getFullName()+" "+getDesignation());
}
return getDropItems();
}
}
My TaskServices.java class in which i run my query is:
public class TaskServices {
public List<EmployeeDutySchedule> dropDownList()
{
List<EmployeeDutySchedule> empDutyList=new ArrayList<EmployeeDutySchedule>();
EmployeeDutySchedule empDuty=new EmployeeDutySchedule();
ResultSet rs=null;
String query="Select emp_id, first_name, last_name, emp_designation FROM transport_department_schema.employees_information;";
System.out.println(query);
rs=MyQueryExe.executeQuery(query);
System.out.println("rs "+rs);
try
{
while(rs.next())
{
empDuty.setEmpID(rs.getString("emp_id"));
empDuty.setFirstName(rs.getString("first_name"));
empDuty.setLastName(rs.getString("last_name"));
empDuty.setDesignation(rs.getString("emp_designation"));
empDutyList.add(empDuty);
System.out.println("size of list "+empDutyList.size());
}
}
catch(SQLException e)
{
e.getStackTrace();
}
return empDutyList;
}
I am confident that there is no error in my services class. Although i am not too sure about how I have written the bean code. While running the page, it shows the arrow for the drop down, but doesn't show any elements.
I referenced: How to populate options of h:selectOneMenu from database?
However I wasnt able to follow it with full understanding.
Please Help :) Thank You in advance :)
edit: this is the source code of the xhtml page when you run it
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TEST PAGE</title>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" type="text/css" href="../CSS/MainCSS.css" />
<link rel="stylesheet" type="text/css" href="../CSS/CompleteTemplateCSS.css" />
<link rel="stylesheet" type="text/css" href="../CSS/templateCSS.css" />
</head>
<body>
<form id="j_idt2" name="j_idt2" method="post" action="/WorkAllocationSystem/webpages/DropDownMenu.xhtml" enctype="application/x-www-form- urlencoded">
<input type="hidden" name="j_idt2" value="j_idt2" />
<select name="j_idt2:j_idt3" size="1"></select><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-8198943631487217003:5203237103046253631" autocomplete="off" />
</form>
</body>
</html>