java - 如何使用 JSP 页面在 mysql 中进行编辑?

标签 java mysql jsp

这是 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"> &nbsp; <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/

相关文章:

java - Java Hibernate 主键有问题吗?

java - Glassfish 使用 URL 重写,即使我没有关闭 cookie

php - 在单个 MySQL 查询中使用多个临时表(适用于 phpMyAdmin,但不适用于 PHP)

mysql - LOCK TABLES 表示表未锁定后在 UNION 查询上进行 SELECT

java - 如何获得 <bean :write> value to be used in <html:text size ="?">

java - Spring Batch 输出记录多于输入记录

java - 将类传递给方法以实例化泛型

mysql - 当我根据查看的位置得到不同的结果时,如何找到列的排序规则?

java - 如何在JSP页面中突出显示 Activity 页面链接

java - 无法将 JSP 属性组配置与 jetty-runner 9.1.4 一起使用