add<电脑编辑>
parent
760cbdcb10
commit
e6ea47674d
|
@ -13,7 +13,7 @@ import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@WebServlet(urlPatterns = {"/computerList","/deleteComputer","/addComputer"})
|
@WebServlet(urlPatterns = {"/computerList","/deleteComputer","/addComputer","/editComputer"})
|
||||||
public class ComputerServlet extends HttpServlet {
|
public class ComputerServlet extends HttpServlet {
|
||||||
|
|
||||||
IComputerService computerService = new ComputerServiceImpl();
|
IComputerService computerService = new ComputerServiceImpl();
|
||||||
|
@ -35,9 +35,19 @@ public class ComputerServlet extends HttpServlet {
|
||||||
case "/addComputer":
|
case "/addComputer":
|
||||||
addComputer(req, resp);
|
addComputer(req, resp);
|
||||||
break;
|
break;
|
||||||
|
case "/editComputer":
|
||||||
|
editComputer(req, resp);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void editComputer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
String id = req.getParameter("id");
|
||||||
|
Computer computer = computerService.getComputer(id);
|
||||||
|
req.setAttribute("computer", computer);
|
||||||
|
req.getRequestDispatcher("editComputer.jsp").forward(req, resp);
|
||||||
|
}
|
||||||
|
|
||||||
private void addComputer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
private void addComputer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
req.getRequestDispatcher("addComputer.jsp").forward(req, resp);
|
req.getRequestDispatcher("addComputer.jsp").forward(req, resp);
|
||||||
}
|
}
|
||||||
|
@ -60,6 +70,31 @@ public class ComputerServlet extends HttpServlet {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
String path = req.getServletPath();
|
||||||
|
switch (path) {
|
||||||
|
case "/addComputer":
|
||||||
|
savaComputer(req, resp);
|
||||||
|
break;
|
||||||
|
case "/editComputer":
|
||||||
|
UpdatComputer(req, resp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdatComputer(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
String id = req.getParameter("id");
|
||||||
|
String name = req.getParameter("name");
|
||||||
|
String price = req.getParameter("price");
|
||||||
|
String stock = req.getParameter("stock");
|
||||||
|
computerService.updateComputer(id,name,Double.parseDouble(price),Integer.parseInt(stock));
|
||||||
|
try {
|
||||||
|
resp.sendRedirect("/computerList");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void savaComputer(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||||
String name = req.getParameter("name");
|
String name = req.getParameter("name");
|
||||||
String price = req.getParameter("price");
|
String price = req.getParameter("price");
|
||||||
String stock = req.getParameter("stock");
|
String stock = req.getParameter("stock");
|
||||||
|
|
|
@ -3,6 +3,7 @@ package example.dao;
|
||||||
import example.model.Computer;
|
import example.model.Computer;
|
||||||
import example.utils.DBUtils;
|
import example.utils.DBUtils;
|
||||||
import org.apache.commons.dbutils.QueryRunner;
|
import org.apache.commons.dbutils.QueryRunner;
|
||||||
|
import org.apache.commons.dbutils.handlers.BeanHandler;
|
||||||
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
import org.apache.commons.dbutils.handlers.BeanListHandler;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
@ -46,9 +47,13 @@ public class ComputerDao {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Computer getComputerById(int i) {
|
||||||
|
String sql = "select * from computer where id=?";
|
||||||
|
try {
|
||||||
|
return queryRunner.query(sql, new BeanHandler<>(Computer.class), i);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,8 @@ public interface IComputerService {
|
||||||
List<Computer> listComputer() throws SQLException;
|
List<Computer> listComputer() throws SQLException;
|
||||||
|
|
||||||
void deleteComputer(String id);
|
void deleteComputer(String id);
|
||||||
|
|
||||||
|
Computer getComputer(String id);
|
||||||
|
|
||||||
|
void updateComputer(String id, String name, double v, int i);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,14 @@ public class ComputerServiceImpl implements IComputerService {
|
||||||
public void deleteComputer(String id) {
|
public void deleteComputer(String id) {
|
||||||
computerDao.deleteComputer(Integer.parseInt(id));
|
computerDao.deleteComputer(Integer.parseInt(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Computer getComputer(String id) {
|
||||||
|
return computerDao.getComputerById(Integer.parseInt(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateComputer(String id, String name, double v, int i) {
|
||||||
|
computerDao.updateComputer(name, v, i, Integer.parseInt(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
<td>${computer.price}</td>
|
<td>${computer.price}</td>
|
||||||
<td>${computer.stock}</td>
|
<td>${computer.stock}</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="/computer?action=updateForm&id=${computer.id}">Edit</a> |
|
<a href="/editComputer?id=${computer.id}">Edit</a> |
|
||||||
<a href="/deleteComputer?id=${computer.id}">Delete</a>
|
<a href="/deleteComputer?id=${computer.id}">Delete</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Update Computer</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Update Computer</h1>
|
||||||
|
<form action="/editComputer" method="post">
|
||||||
|
<input type="hidden" name="id" value="${computer.id}">
|
||||||
|
<label for="name">Name: </label><br>
|
||||||
|
<input type="text" id="name" name="name" value="${computer.name}"><br><br>
|
||||||
|
<label for="price">Price: </label><br>
|
||||||
|
<input type="text" id="price" name="price" value="${computer.price}"><br><br>
|
||||||
|
<label for="stock">Stock: </label><br>
|
||||||
|
<input type="text" id="stock" name="stock" value="${computer.stock}"><br><br>
|
||||||
|
<input type="submit" value="Update Computer">
|
||||||
|
</form>
|
||||||
|
<br>
|
||||||
|
<a href="/computerList">Back to List</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue