如何使用JSP页面在mysql中编辑?

时间:2016-08-29 11:59:06

标签: 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> 

因此,当我点击编辑按钮时,它将重定向到员工详细信息页面,我获得了特定的员工详细信息。

但是在编辑了特定员工的详细信息并点击了“更新”按钮后, 我没有白色屏幕。 那个页面网址是:: “http://localhost:8000/SampleLogin/EmployeeController

那么我怎样才能解决这个问题并继续前进。

2 个答案:

答案 0 :(得分:2)

您需要做的就是替换

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

with:&lt; input type="submit" name="action" value="edit" class="btn btn-primary">

答案 1 :(得分:0)

首先 - 不要将 doGet 用于更新数据。

您的表单方法是 POST

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

因此,您需要在控制器的 doPost 方法中执行所有更新操作。

以下是有关使用servlet的CRUD的一些有用提示 - https://stackoverflow.com/tags/servlets/info