這是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"> <input type="reset" name="cancel" value="Cancel" class="btn btn-primary">
</div>
</div>
</form>
所以當我點擊編輯按鈕時,它將被重定向到員工詳細信息頁面,我得到了具體的員工詳細信息。
但編輯後,特定的員工細節,並點擊「更新」按鈕, 我得到了白色的屏幕,沒有別的。 那個頁面的網址是: 「http://localhost:8000/SampleLogin/EmployeeController」
所以我該如何解決這個問題,並繼續前進。
看@Ninja編碼器的答案 - 他是對的,至少你在html中有一個錯誤。而對於更清晰的編碼實踐,最好將doGet和doPost方法之間的讀取和寫入請求分開處理。 –