【问题标题】:Load value from database to <h:inputText> with <h:selectOneMenu> valueChangeEvent使用 <h:selectOneMenu> valueChangeEvent 将值从数据库加载到 <h:inputText>
【发布时间】:2013-12-20 00:05:24
【问题描述】:

我想开发一个 JSF 页面,让用户从数据库中编辑员工。我在 h:selectOneMenu 中加载了所有员工的列表。第二件事我希望 valueChangeEvent 员工详细信息应该加载到相应的 h:inputText 中。但是每个 valueChangeEvent 都没有发生任何事情。那么,代码的问题在哪里。

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <div id="hdr">
        <h1>Vinweb Services</h1>
        <hr/>
    </div>
    ;
    <div id="mnu" style="height: 50px; width: auto; background: skyblue;">
        <h:form>
        <h:commandLink value="Home" action="index"/>
        <h:outputText value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/>
        <h:commandLink value="Create" action="create"/>
        <h:outputText value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/>
        <h:commandLink value="View" action="view"/>
        <h:outputText value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/>
        <h:commandLink value="Edit" action="edit"/>
        </h:form>
    </div>

    <div id="cnt">
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel value="Select an Employee"/>
                <h:selectOneMenu value="#{esb.empName}" onchange="submit()"  valueChangeListener="#{esb.loadEmployeeDetail}">
                <f:selectItems value="#{esb.employeeList}"/>
                </h:selectOneMenu>

                <h:outputLabel value="Employee Code"/>
                <h:inputText value="#{esb.empCode}" required="true"/>

                <h:outputLabel value="Employee Name"/>
                <h:inputText value="#{esb.empName}" required="true"/>

                <h:outputLabel value="Joining Date"/>
                <h:inputText value="#{esb.joinDate}" required="true">
                    <f:convertDateTime pattern="MM/yy"/>
                </h:inputText>

                <h:outputLabel value="Salary"/>
                <h:inputText value="#{esb.salary}" required="true"/>

                <h:commandButton value="Update" action="#{esb.updateEmployeeDetail}"/>
            </h:panelGrid>
        </h:form>            
    </div>

</h:body>
</html>

支持 Bean:

package ems.bean;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import javax.annotation.Resource;
import javax.faces.event.ValueChangeEvent;
import javax.sql.DataSource;

@Named(value = "esb")
@SessionScoped
public class EmployeeServiceBean implements Serializable {
@Resource(name = "JPA4A")
private DataSource ds;
// Member Declaration   


private String empCode;
private String empName;
private Date joinDate;
private long salary;
private ArrayList empList;

// Service Code Segment

// This method will publish all emloyees in table to selectOneMenu 
public ArrayList getEmployeeList() throws SQLException{
   empList = new ArrayList();
   Connection con = ds.getConnection();
   try{           
       Statement stmt =  con.createStatement();
       ResultSet rslt = stmt.executeQuery("SELECT empName FROM Employee");

       while(rslt.next()){
           empList.add(rslt.getString("empName"));
       }
   }
   catch(SQLException se) {
       throw new SQLException();
   }
   finally{
       con.close();
   }
   return empList;
} 

// This method should load selected empoyee's details in inputText fields
public void loadEmployeeDetail(ValueChangeEvent evt) throws SQLException{
   String emp = evt.getNewValue().toString();

   Connection con = ds.getConnection();
   try{           
       Statement stmt =  con.createStatement();
       String qry = "SELECT * FROM Employee WHERE empName = '"+emp+"'";
       ResultSet rslt = stmt.executeQuery(qry);
       rslt.next();
       this.empCode = rslt.getString("empCode");
       this.empName = rslt.getString("empName");
       this.joinDate = rslt.getDate("joinDate");
       this.salary = rslt.getLong("salary");
   }
   catch(SQLException se) {
        se.printStackTrace();
   }
   finally{
       con.close();
   }

 }

 public void updateEmployeeDetail() throws SQLException{       
   Connection con = ds.getConnection();
   try{           
       Statement stmt =  con.createStatement();
       boolean rs = stmt.execute("UPDATE Employee SET empCode='"+empCode+"',  empName='"+empName+"', joinDate='"+joinDate+"', salary="+salary);
   }
   catch(SQLException se) {
       throw new SQLException();
   }
   finally{
       con.close();
   }

}
// Property Getter & Setter code segment
public String getEmpCode() {
    return empCode;
}

public void setEmpCode(String empCode) {
    this.empCode = empCode;
}

public String getEmpName() {
    return empName;
}

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

public Date getJoinDate() {
    return joinDate;
}

public void setJoinDate(Date joinDate) {
    this.joinDate = joinDate;
}

public long getSalary() {
    return salary;
}

public void setSalary(long salary) {
    this.salary = salary;
}




}

【问题讨论】:

  • 如果不查看您的代码,很难判断您哪里出错了。
  • 我需要帮助,因为这个问题阻止了我继续前进,其次我是新手。

标签: jsf-2


【解决方案1】:

valueChangeListener 不适合这项工作。

你在这里做一个onchange="submit()" 提交整个表单,包括那些反过来导致验证错误的必填字段,而这些错误又没有引起你的注意,因为你没有任何&lt;h:message(s)&gt; 给他们。如果您在表单中放置了&lt;h:messages/&gt;,或者更喜欢和关注服务器日志,那么您应该会收到required="true" 验证错误的通知。

你需要&lt;f:ajax listener&gt;

替换

<h:selectOneMenu value="#{esb.empName}" onchange="submit()"  valueChangeListener="#{esb.loadEmployeeDetail}">
    <f:selectItems value="#{esb.employeeList}"/>
</h:selectOneMenu>

通过

<h:selectOneMenu value="#{esb.empName}">
    <f:selectItems value="#{esb.employeeList}"/>
    <f:ajax listener="#{esb.loadEmployeeDetail}" render="@form" />
</h:selectOneMenu>

替换

public void loadEmployeeDetail(ValueChangeEvent evt) throws SQLException{
   String emp = evt.getNewValue().toString();
   // ...
}

通过

public void loadEmployeeDetail() throws SQLException{
   String emp = empName; // You can also just use `empName` directly.
   // ...
}

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多