gpt4 book ai didi

java - SelectOneMenu 没有任何可供选择的值

转载 作者:行者123 更新时间:2023-11-30 00:24:01 24 4
gpt4 key购买 nike

当我尝试显示employee.jsp 页面时,我无法选择员工。看起来 SelectOneMenu 不起作用。我是否遗漏了该标签中的某些内容(第 20 行)?

我检查过,数据库中有员工表,其中包含一些员工数据。我已连接到数据库。我使用mysql数据库。您可以在 persistance.xml 文件中看到与此数据库的连接。


<%@ page language="java" contentType="text/html; 
charset=US-ASCII" pageEncoding="US-ASCII"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
<f:view>
<h2><center>Welcome to Employee Home Page</center></h2>
<br><br>
<h:form>
<h2>
Select an Employee Number from the drop down:
</h2>
<br><br>
<h:selectOneMenu id="selEmpNo"
valueChangeListener="#{employee.employeeReport}"
onchange="submit()">
<f:selectItem itemLabel="select" />
<f:selectItems value="#{employee.empNoList}" id="emp"/>
</h:selectOneMenu>
</h:form>
<br><br>
<h2>
<h:outputText value="Employee Name: "></h:outputText>
<h:outputText value="#{employee.empName}"/>
<br><br>
<h:outputText value="Employee Number: "/>
<h:outputText value="#{employee.empNo}"/>
<br><br>
<h:outputText value="Name of the IBU: "/>
<h:outputText value="#{employee.ibu}"/>
<br><br>
<h:outputText value="Designation: "/>
<h:outputText value="#{employee.designation}"/> </h2>
</f:view>
</body>
</html>


<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>


<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Home</title>
</head>
<body>
<f:view>
<h2>
<center>
<h:outputLabel> Welcome!!!</h:outputLabel>
</center>
<h:outputLink value="employee.jsp">Employee Details</h:outputLink>
</h2>
</f:view>
</body>
</html>
</html>


package employee;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

public class EmployeeBean {

private String empName;
private String ibu;
private String designation;
private int empNo;
//This is used to dynamically populate the drop down with employee numbers
List<SelectItem> empNoList;
List<EmployeeEntity>empList;

public EmployeeBean(){
this.empNoList = new ArrayList<SelectItem>();
/*Populating Employee Number in the drop down - Dynamic */
empList = new EmployeeService().getEmployeeList();
Iterator<EmployeeEntity>iterator = empList.iterator();
while(iterator.hasNext()){
EmployeeEntity employee = iterator.next();
SelectItem item = new SelectItem(employee.getEmpNo());
empNoList.add(item);
}
System.out.println(empNoList);
}

public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}

public String getIbu() {
return ibu;
}

public void setIbu(String ibu) {
this.ibu = ibu;
}

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

public int getEmpNo() {
return empNo;
}

public void setEmpNo(int empNo) {
this.empNo = empNo;
}

public List<SelectItem> getEmpNoList() {
return empNoList;
}

public void setEmpNoList(List<SelectItem> empNoList) {
this.empNoList = empNoList;
}

public List<EmployeeEntity> getEmpList() {
return empList;
}

public void setEmpList(List<EmployeeEntity> empList) {
this.empList = empList;

}
/*Eventlistener - for fetching an employee record based on the selection of employee
number from the drop down*/
public void employeeReport(ValueChangeEvent event){
int empNo = Integer.parseInt((String)event.getNewValue());
EmployeeEntity employee = new EmployeeService().getEmployee(empNo);
this.empNo = employee.getEmpNo();
this.empName = employee.getEmpName();
this.ibu = employee.getIbu();
this.designation = employee.getDesignation();
}
}


  package employee;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class EmployeeEntity {

@Id
private int empNo;
private String empName;
private String ibu;
private String designation;

public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getIbu() {
return ibu;
}
public void setIbu(String ibu) {
this.ibu = ibu;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}


package employee;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class EmployeeService {

public List<EmployeeEntity> getEmployeeList(){
List<EmployeeEntity> empList = new ArrayList<EmployeeEntity>();
EntityManager em = null;
try{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("Employee-
Details");
em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();
Query query = em.createQuery("select empNo from employee ");
empList = query.getResultList();

}
catch(Exception e){
//log the exception
}
return empList;
}

public EmployeeEntity getEmployee(int empNo){
EmployeeEntity employee = new EmployeeEntity();
EntityManager em = null;
try{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("Employee-Details");
em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
employee = em.find(EmployeeEntity.class, empNo);
}
catch(Exception e){
//log the exception
}
finally{
if( em != null){
em.clear();
}
}
return employee;
}
}


<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

<navigation-rule>
<display-name>Employee</display-name>
<from-view-id>/index.jsp</from-view-id>
</navigation-rule>
<managed-bean>
<managed-bean-name>employee</managed-bean-name>
<managed-bean-class>employee.EmployeeBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Generating Employee Report</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


enter image description here enter image description here enter image description here enter image description here

最佳答案

您应该将 value 添加到 selectOneMenuvalueChangeListener 属性接受表示值更改监听器方法的方法绑定(bind)表达式当为此输入组件设置新值时收到通知。值更改监听器方法必须是采用 ValueChangeEvent 参数且返回类型为 void 的公共(public)方法。它与 value 属性不同。

您的页面。

<h:selectOneMenu id="selEmpNo"
valueChangeListener="#{employee.employeeReport}"
onchange="submit()" value="#{employee.myvalue}">
<f:selectItem itemLabel="select" />
<f:selectItems value="#{employee.empNoList}" id="emp"/>
</h:selectOneMenu>

员工Bean

    package employee;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

public class EmployeeBean {

private String myValue;
private String empName;
private String ibu;
private String designation;

public String getMyValue() {
return myValue;
}

public void setMyValue(String myValue) {
this.myValue = myValue;
}

问候

来源here

关于java - SelectOneMenu 没有任何可供选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23022912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com