这是 EmployeeDao.java 文件。
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>
所以当我点击编辑按钮时,它将被重定向到员工详情页面,我得到了具体的员工详情。
但是在编辑特定员工详细信息并单击“更新”按钮后, 我得到了白屏,没有别的。 该页面 url 是:: "http://localhost:8000/SampleLogin/EmployeeController "
那么我该如何解决这个问题并继续前进。
最佳答案
你需要做的就是替换
<input type="submit" name="submit" value="Update" class="btn btn-primary">
使用:<input type="submit"name="action"value="edit"class="btn btn-primary">
关于java - 如何使用 JSP 页面在 mysql 中进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39205808/