0

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>
Community
  • 1
  • 1
Shruti
  • 97
  • 1
  • 4
  • 14
  • f:selectItems value should be a list of values. You point it to a single string member 'fullname'. It cannot work as is. f:selectItem and f:selectitems are two different tags with different parameters. You should use proper getters/setters : List getItems() {...} on java side, and f:selectitems with value="#{bean.items}" on xhtml side. – Stephane Lallemagne Apr 15 '14 at 10:35
  • i changed it to : ' ' – Shruti Apr 15 '14 at 11:04
  • it still doesnt work... now I am passing a list of all the empID from my db – Shruti Apr 15 '14 at 11:06
  • You will get an `IndexOutOfBoundsException` in the for loop, as the following is possible `i=getDropItems().size()`. – user1983983 Apr 15 '14 at 12:03

1 Answers1

0

You have wrong type of object used for f:selectItems. It should be list (you have String) like List<SelectItem>. Form list like list.add(new SelectItem(objectOrId, label));

Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • could you please elaborate. Also I want the add() to be dynamically added. It depends on the rows in the database. I hope you know what I mean. How should i take care of that? and if you see in the above answer. i have changed it to list type. Still it didn't work. :/ – Shruti Apr 15 '14 at 17:31
  • Read data from database and form the list of `SelectItem`. In your code you have simple `selectOneMenu` which filed ones. Try this solution or post proper code. – Vasil Lukach Apr 15 '14 at 17:38