1

I want to develop a JSF Page which lets user edit a Employee from database. I loaded list of all employees in h:selectOneMenu. and second thing i want that with valueChangeEvent employee detail should be loaded in corresponding h:inputText. but nothing happens with every valueChangeEvent. So, where is the problem in codes.

<?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>

Backing 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;
}




}
2
  • Without looking at your code it's very hard to tell where you went wrong.
    – SRy
    Dec 3, 2013 at 21:05
  • I need help because this problem has stopped me going further, secondly i am newbee.
    – vinweb
    Dec 5, 2013 at 16:18

1 Answer 1

1

The valueChangeListener is the wrong tool for the job.

You're here doing a onchange="submit()" which submits the entire form, including those required fields which in turn caused validation errors which in turn failed to reach your attention because you don't have any <h:message(s)> for them. If you have placed a <h:messages/> inside the form, or have paid more love and attention to server logs, you should have been notified of those required="true" validation errors.

You need <f:ajax listener>.

Replace

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

by

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

and replace

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

by

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

See also:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.