2016-08-29 76 views
1

這是EmployeeDao.java文件。如何在使用JSP頁面的mysql中進行編輯?

public Employee getEmployeeById(String employeeId){ 
    System.out.println("IN getEmployeeById"); 
    Employee employee = new Employee(); 
    try { 
     PreparedStatement preparedStatement = conn.prepareStatement("select * from login where email=?"); 
     preparedStatement.setString(1, employeeId); 
     ResultSet rs = preparedStatement.executeQuery(); 
     System.out.println(rs); 
     if(rs.next()){ 
      employee.setFirstname(rs.getString("firstName")); 
      System.out.println(""+employee.getFirstname()); 
      employee.setLastname(rs.getString("lastName")); 
      System.out.println(""+employee.getLastname()); 
      employee.setEmail(rs.getString("email")); 
      System.out.println(""+employee.getEmail()); 
      employee.setStatus(rs.getBoolean("status")); 
     } 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return employee;  
} 

這是EmployeeController.java文件。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    String forward = ""; 
    String action = request.getParameter("action"); 

    if(action.equalsIgnoreCase("delete")){ 
     String employeeId = request.getParameter("employeeId"); 
     dao.deleteEmployee(employeeId); 
     forward = employee_listing ; 
     request.setAttribute("employees", dao.getAllEmployees()); 
    }else if(action.equalsIgnoreCase("edit")){ 
     forward = employee_detail ; 
     String employeeId = request.getParameter("employeeId"); 
     Employee employee = dao.getEmployeeById(employeeId); 
     request.setAttribute("employee", employee); 
    }else if(action.equalsIgnoreCase("listEmployee")){ 
     forward = employee_listing; 
     request.setAttribute("employees", dao.getAllEmployees()); 
    } 
    else{ 
     forward = dashboard; 
    } 

    RequestDispatcher view = request.getRequestDispatcher(forward); 
    view.forward(request, response); 
} 

這是員工詳細信息頁面。

<form class="form-horizontal" method="post" action="EmployeeController" name="frmAddEmployee"> 
           <% 
            String action = request.getParameter("action"); 
            System.out.println(action); 
           %> 
            <% while (rs.next()) { %> 

           <div class="form-group"> 
            <label for="" class="col-sm-4 control-label">First Name</label> 
            <div class="col-sm-8"> 
             <input type="text" class="form-control" id="" value="<%= rs.getString("firstName") %>" > 
            </div> 
           </div> 
           <div class="form-group"> 
            <label for="" class="col-sm-4 control-label">Last Name</label> 
            <div class="col-sm-8"> 
             <input type="text" class="form-control" id="" value="<%= rs.getString("lastName") %>" > 
            </div> 
           </div> 
           <div class="form-group"> 
            <label for="" class="col-sm-4 control-label">Email</label> 
            <div class="col-sm-8"> 
             <input type="email" class="form-control" id="" value="<%= rs.getString("email") %>" > 
             <% } %> 
            </div> 
           </div> 
           <div class="form-group"> 
            <label class="col-sm-4 control-label">Employee Status</label> 
            <div class="col-sm-8"> 
             <label class="switch"> 
              <input type="checkbox" checked> 
              <div class="slider round"></div> 
             </label> 
            </div> 
           </div> 
           <div class="form-group"> 
            <div class="col-sm-4"></div> 
            <div class="col-sm-8"> 
             <input type="submit" name="submit" value="Update" class="btn btn-primary"> &nbsp; <input type="reset" name="cancel" value="Cancel" class="btn btn-primary"> 
            </div> 
           </div> 
          </form> 

所以當我點擊編輯按鈕時,它將被重定向到員工詳細信息頁面,我得到了具體的員工詳細信息。

但編輯後,特定的員工細節,並點擊「更新」按鈕, 我得到了白色的屏幕,沒有別的。 那個頁面的網址是: 「http://localhost:8000/SampleLogin/EmployeeController

所以我該如何解決這個問題,並繼續前進。

+0

看@Ninja編碼器的答案 - 他是對的,至少你在html中有一個錯誤。而對於更清晰的編碼實踐,最好將doGet和doPost方法之間的讀取和寫入請求分開處理。 –

回答

2

所有你需要做的是更換

<input type="submit" name="submit" value="Update" class="btn btn-primary"> 

有:< input type="submit" name="action" value="edit" class="btn btn-primary">

+0

它不會修復原始問題,因爲** doGet **無法通過** POST **方法到達 –

+0

doPost在內部doGet中調用(如果不是相關的自動生成的存根)。在他的jsp中沒有用動作命名的輸入參數,所以他只需要替換他的編輯按鈕名稱和值 –

+0

但doPost不會調用doGet,並且servlet收到doPost –

0

首先 - 不要使用doGet獲取更新數據。

你的形式方法是POST

<form class="form-horizontal" method="post" ...> 

所以,你需要做的內部的doPost控制器的方法中的所有更新magick。

下面是關於使用Servlet CRUD一些有用的提示 - https://stackoverflow.com/tags/servlets/info

相關問題