5.10. Editing a CarTo edit an existing car, we'll add another set of links on the viewCarList page in Figure 5-4. Figure 5-4. Editing carsThe Edit links each contain the ID of the displayed car. They call the ControllerServlet using the editCar action. Example 5-21 shows the JSP code. Example 5-21. carList.jsp<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <link rel="stylesheet" type="text/css" href="default.css"> </head> <body> <p><a href="controller?action=addCar">[Add Car]</a></p> <table> <tr> <th>Action</th> <th>Make</th> <th>Model</th> <th >Model Year</th> </tr> <c:forEach items='${carList}' var='car'> <tr> <td><a href="controller?action=editCar&id=${car.id}">Edit</a></td> <td>${car.make}</td> <td>${car.model}</td> <td >${car.modelYear}</td> </tr> </c:forEach> </table> </body> </html> The ControllerServlet in Example 5-22 catches the request, queries the requested CarDTO out of the database using the ID, places it in the Request scope, and finally redirects to the carForm.jsp. Example 5-22. ControllerServlet.java// perform action if(VIEW_CAR_LIST_ACTION.equals(actionName)) { CarDAO carDAO = new HibernateCarDAO( ); request.setAttribute("carList", carDAO.findAll( )); destinationPage = "/carList.jsp"; } else if(ADD_CAR_ACTION.equals(actionName)) { request.setAttribute("car", new CarDTO( )); destinationPage = "/carForm.jsp"; } else if(EDIT_CAR_ACTION.equals(actionName)) { int id = Integer.parseInt(request.getParameter("id")); CarDAO carDAO = new HibernateCarDAO( ); request.setAttribute("car", carDAO.findById(id)); destinationPage = "/carForm.jsp"; } else if(SAVE_CAR_ACTION.equals(actionName)) { //build the car from the request parameters CarDTO car = new CarDTO( ); car.setMake(request.getParameter("make")); car.setModel(request.getParameter("model")); car.setModelYear(request.getParameter("modelYear")); //save the car CarDAO carDAO = new HibernateCarDAO( ); carDAO.create(car); //prepare the list request.setAttribute("carList", carDAO.findAll( )); destinationPage = "/carList.jsp"; } else { String errorMessage = "[" + actionName + "] is not a valid action."; request.setAttribute(ERROR_KEY, errorMessage); } This time having a filled-in CarDTO in Request scope allows us to populate the form in Figure 5-5 with the appropriate data. Figure 5-5. carForm.jsp used to edit a CarDTOThe "Save" button still sends the form data back to the ControllerServlet using the saveCar action, only this time the ID value is a positive integer. (Recall that CarDTO.id is initialized to "-1" unless you provide a specific value.) This allows us to reuse the saveCar method for both inserts and updates. Example 5-23 shows the modified method in ControllerServlet. Example 5-23. ControllerServlet.java// perform action if(VIEW_CAR_LIST_ACTION.equals(actionName)) { CarDAO carDAO = new HibernateCarDAO( ); request.setAttribute("carList", carDAO.findAll( )); destinationPage = "/carList.jsp"; } else if(ADD_CAR_ACTION.equals(actionName)) { request.setAttribute("car", new CarDTO( )); destinationPage = "/carForm.jsp"; } else if(EDIT_CAR_ACTION.equals(actionName)) { int id = Integer.parseInt(request.getParameter("id")); CarDAO carDAO = new HibernateCarDAO( ); request.setAttribute("car", carDAO.findById(id)); destinationPage = "/carForm.jsp"; } else if(SAVE_CAR_ACTION.equals(actionName)) { //build the car from the request parameters CarDTO car = new CarDTO( ); car.setId(Integer.parseInt(request.getParameter("id"))); car.setMake(request.getParameter("make")); car.setModel(request.getParameter("model")); car.setModelYear(request.getParameter("modelYear")); //save the car CarDAO carDAO = new HibernateCarDAO( ); if(car.getId( ) = = -1) { carDAO.create(car); } else { carDAO.update(car); } //prepare the list request.setAttribute("carList", carDAO.findAll( )); destinationPage = "/carList.jsp"; } else { String errorMessage = "[" + actionName + "] is not a valid action."; request.setAttribute(ERROR_KEY, errorMessage); } Which, of course, brings us back to the DAO. Example 5-24 shows what the update code looks like in JDBCCarDAO. Example 5-24. JDBCCarDAO.javapublic void update(CarDTO car) { DataSource dataSource = null; Connection conn = null; PreparedStatement pstmt = null; String insertSql = "update CAR set MAKE=?, MODEL=?, MODEL_YEAR=? where id=?"; try { dataSource = ServiceLocator.getDataSource(DATA_SOURCE); conn = dataSource.getConnection( ); pstmt = conn.prepareStatement(insertSql); pstmt.setString(1, car.getMake( )); pstmt.setString(2, car.getModel( )); pstmt.setString(3, car.getModelYear( )); pstmt.setInt(4, car.getId( )); pstmt.executeUpdate( ); } catch (Exception e) { System.out.println(e); } finally { try { if(pstmt != null){pstmt.close( );} if(conn != null){conn.close( );} } catch(Exception e) { System.out.println(e); } } } And Example 5-25 shows the corresponding reduced code in HibernateCarDAO. Example 5-25. HibernateCarDAO.javapublic void update(CarDTO car) { Session session = null; Transaction tx = null; try { session = ServiceLocator.getHibernateSession(HIBERNATE_SESSION_FACTORY); tx = session.beginTransaction( ); session.update(car); tx.commit( ); } catch (Exception e) { try{tx.rollback( );} catch(Exception e2){System.out.println(e2);} System.out.println(e); } finally { try { if (session != null) {session.close( );} } catch (Exception e) { System.out.println(e); } } }
Use the web interface to make changes to the cars in the database. |