2015-02-12 86 views
0

我是JSF中的新手,在获取下拉列表时遇到问题。我不想使用@PostConstructor。我在谷歌上尝试了几个来源,但我不知道我犯了什么错误。请把我拉出来。JSF selectOneMenu

Managed Bean的

package com.employee.registration; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean 
@SessionScoped 
public class EmployeeBean implements Serializable { 
    private static final long serialVersionUID = -5400118477202860998L; 
    private String firstName; 
    private String lastName; 
    private String emailID; 
    private int employeeNumber; 
    private String employeeDepartment; 
    private String dplist; 
    private List<DepartmentList> departmentList; 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getEmailID() { 
     return emailID; 
    } 

    public void setEmailID(String emailID) { 
     this.emailID = emailID; 
    } 

    public int getEmployeeNumber() { 
     return employeeNumber; 
    } 

    public void setEmployeeNumber(int employeeNumber) { 
     this.employeeNumber = employeeNumber; 
    } 

    public String getEmployeeDepartment() { 
     return employeeDepartment; 
    } 

    public void setEmployeeDepartment(String employeeDepartment) { 
     this.employeeDepartment = employeeDepartment; 
    } 

    public List<DepartmentList> getDepartmentList() { 
     return departmentList; 
    } 

    public void setDepartmentList(List<DepartmentList> departmentList) { 
     this.departmentList = departmentList; 
    } 

    public List<DepartmentList> getDepartments() { 
     departmentList = new ArrayList<DepartmentList>(); 
     departmentList.add(new DepartmentList("1", "Finance")); 
     departmentList.add(new DepartmentList("2", "Bnking")); 
     return departmentList; 
    } 

    public String getDplist() { 
     return dplist; 
    } 

    public void setDplist(String dplist) { 
     this.dplist = dplist; 
    } 

} 

的Java类

package com.employee.registration; 

public class DepartmentList { 

private String departmentId; 
private String departmentName; 

public DepartmentList(String departmentId, String departmentName) { 
    this.departmentId = departmentId; 
    this.departmentName = departmentName; 
} 

public String getDepartmentId() { 
    return departmentId; 
} 

public void setDepartmentId(String departmentId) { 
    this.departmentId = departmentId; 
} 

public String getDepartmentName() { 
    return departmentName; 
} 

public void setDepartmentName(String departmentName) { 
    this.departmentName = departmentName; 
} 

}


XHTML

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<h:head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <title>Employee Registration</title> 
    <h:outputStylesheet library="css" name="stylesheet.css" /> 
</h:head> 
<h:body> 
    <h:form> 
     <fieldset> 
      <legend>Enter the Employees Information </legend> 

      <h:outputLabel id="firstName" value="First Name :" for="fName"></h:outputLabel> 
      <h:inputText id="fName" value="#{employeeBean.firstName}" 
       required="true"></h:inputText> 
      <br></br> 

      <h:outputLabel id="lastName" value="Last Name :" for="lName"></h:outputLabel> 
      <h:inputText id="lName" value="#{employeeBean.lastName}" 
       required="true"></h:inputText> 
      <br></br> 

      <h:outputLabel id="emailId" value="Email ID :"></h:outputLabel> 
      <h:inputText id="email" value="#{employeeBean.emailID}" 
       required="true"></h:inputText> 
      <br></br> 

      <h:outputLabel id="employeeNumberId" value="Employee Number :"></h:outputLabel> 
      <h:inputText id="empNumber" value="#{employeeBean.employeeNumber}"></h:inputText> 
      <br></br> 

      <h:outputLabel id="employeeDepartmentID" value="Employee Department"></h:outputLabel> 
      <h:inputText id="eDepartment" 
       value="#{employeeBean.employeeDepartment}"></h:inputText> 
      <br></br> 

      <h:selectOneMenu value="#{employeeBean.dplist}"></h:selectOneMenu> 
      <f:selectItems value="#{employeeBean.departmentList}" var="e" 
       itemLabel="#{e.departmentId}" itemValue="#{e.departmentName}"></f:selectItems> 
      <h:commandButton id="submit" value="Submit" 
       action="outputInformation"></h:commandButton> 

     </fieldset> 
    </h:form> 
</h:body> 
</html> 
+0

除此之外重复的,你最好也读http://stackoverflow.com/q/7031885和http://stackoverflow.com/q/2090033来了解当前代码的错误和低效率。 – BalusC 2015-02-12 08:43:34

+0

而且,对于将来的问题,请仔细阅读http://stackoverflow.com/help/mcve如何获得正确的代码片段。例如。你的bean中的所有其他字段以及'
'和''等标签肯定不会导致实际问题,因此只会导致噪音。 – BalusC 2015-02-12 08:44:58

回答

0

selectItems标签应该是内部selectOneMenu用于:)

 <h:selectOneMenu value="#{employeeBean.dplist}}"> 
      <f:selectItems value="#{employeeBean.departmentList}" var="e" 
      itemLabel="#{e.departmentId}" itemValue="#{e.departmentName}" /> 
     </h:selectOneMenu> 

参见教程here

相关问题