fix<路径调整>

master
liyansheng 2024-12-21 23:38:06 +08:00
parent 91491824ec
commit cd1fd254d7
6 changed files with 26 additions and 25 deletions

View File

@ -65,7 +65,7 @@ public class CartServlet extends HttpServlet {
User user = (User) req.getSession().getAttribute("user"); User user = (User) req.getSession().getAttribute("user");
cartService.addCart(Integer.parseInt(id),user.getId()); cartService.addCart(Integer.parseInt(id),user.getId());
req.setAttribute("msg","加购成功"); req.setAttribute("msg","加购成功");
req.getRequestDispatcher("/computerList").forward(req,resp); req.getRequestDispatcher("/computer?action=list").forward(req,resp);
} }
private void toCart(HttpServletRequest req, HttpServletResponse resp) throws Exception { private void toCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {

View File

@ -10,32 +10,33 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
@WebServlet(urlPatterns = {"/computerList","/deleteComputer","/addComputer","/editComputer"}) @WebServlet(urlPatterns = {"/computer"})
public class ComputerServlet extends HttpServlet { public class ComputerServlet extends HttpServlet {
IComputerService computerService = new ComputerServiceImpl(); IComputerService computerService = new ComputerServiceImpl();
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = req.getServletPath(); String action = req.getParameter("action");
switch (path) { switch (action) {
case "/computerList": case "list":
try { try {
listServlet(req, resp); listServlet(req, resp);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
break; break;
case "/deleteComputer": case "delete":
deleteComputer(req, resp); deleteComputer(req, resp);
break; break;
case "/addComputer": case "add":
addComputer(req, resp); addComputer(req, resp);
break; break;
case "/editComputer": case "edit":
editComputer(req, resp); editComputer(req, resp);
break; break;
} }
@ -56,7 +57,7 @@ public class ComputerServlet extends HttpServlet {
String id = req.getParameter("id"); String id = req.getParameter("id");
try { try {
computerService.deleteComputer(id); computerService.deleteComputer(id);
resp.sendRedirect("/computerList"); resp.sendRedirect("/computer?action=list");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -70,25 +71,25 @@ 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(); String action = req.getParameter("action");
switch (path) { switch (action) {
case "/addComputer": case "add":
savaComputer(req, resp); savaComputer(req, resp);
break; break;
case "/editComputer": case "edit":
UpdatComputer(req, resp); UpdateComputer(req, resp);
break; break;
} }
} }
private void UpdatComputer(HttpServletRequest req, HttpServletResponse resp) { private void UpdateComputer(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
String id = req.getParameter("id"); String id = req.getParameter("id");
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");
computerService.updateComputer(id,name,Double.parseDouble(price),Integer.parseInt(stock)); computerService.updateComputer(id,name,Double.parseDouble(price),Integer.parseInt(stock));
try { try {
resp.sendRedirect("/computerList"); resp.sendRedirect("/computer?action=list");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -100,7 +101,7 @@ public class ComputerServlet extends HttpServlet {
String stock = req.getParameter("stock"); String stock = req.getParameter("stock");
computerService.addComputer(name,Double.parseDouble(price),Integer.parseInt(stock)); computerService.addComputer(name,Double.parseDouble(price),Integer.parseInt(stock));
try { try {
resp.sendRedirect("/computerList"); resp.sendRedirect("/computer?action=list");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -9,7 +9,7 @@
</head> </head>
<body> <body>
<h1>电脑产品发布</h1> <h1>电脑产品发布</h1>
<form action="/addComputer" method="post"> <form action="/computer?action=add" method="post">
<label for="name">名称: </label><br> <label for="name">名称: </label><br>
<input type="text" id="name" name="name"><br><br> <input type="text" id="name" name="name"><br><br>
<label for="price">价格: </label><br> <label for="price">价格: </label><br>
@ -19,6 +19,6 @@
<input type="submit" value="确定"> <input type="submit" value="确定">
</form> </form>
<br> <br>
<a href="/computerList">返回电脑列表</a> <a href="/computer?action=list">返回电脑列表</a>
</body> </body>
</html> </html>

View File

@ -31,8 +31,8 @@
<td>${computer.stock}</td> <td>${computer.stock}</td>
<td> <td>
<c:if test="${sessionScope.user.role == 'admin'}"> <c:if test="${sessionScope.user.role == 'admin'}">
<a href="/editComputer?id=${computer.id}">编辑</a> | <a href="/computer?action=edit&id=${computer.id}">编辑</a> |
<a href="/deleteComputer?id=${computer.id}">删除</a> <a href="/computer?action=delete&id=${computer.id}">删除</a>
</c:if> </c:if>
@ -52,7 +52,7 @@
<a href="/cart?action=list">我的购物车</a> <a href="/cart?action=list">我的购物车</a>
</c:if> </c:if>
<c:if test="${sessionScope.user.role == 'admin'}"> <c:if test="${sessionScope.user.role == 'admin'}">
<a href="/addComputer">发布电脑产品</a> <a href="/computer?action=add">发布电脑产品</a>
</c:if> </c:if>
<br> <br>
<a href="/">主页</a> <a href="/">主页</a>

View File

@ -10,7 +10,7 @@
</head> </head>
<body> <body>
<h1>更新电脑信息</h1> <h1>更新电脑信息</h1>
<form action="/editComputer" method="post"> <form action="/computer?action=edit" method="post">
<input type="hidden" name="id" value="${computer.id}"> <input type="hidden" name="id" value="${computer.id}">
<label for="name">名称: </label><br> <label for="name">名称: </label><br>
<input type="text" id="name" name="name" value="${computer.name}"><br><br> <input type="text" id="name" name="name" value="${computer.name}"><br><br>
@ -21,6 +21,6 @@
<input type="submit" value="提交修改"> <input type="submit" value="提交修改">
</form> </form>
<br> <br>
<a href="/computerList">返回电脑列表</a> <a href="/computer?action=list">返回电脑列表</a>
</body> </body>
</html> </html>

View File

@ -6,7 +6,7 @@
<h2>电脑商城-首页</h2> <h2>电脑商城-首页</h2>
<p>你好!${sessionScope.user.username}</p> <p>你好!${sessionScope.user.username}</p>
<c:if test="${sessionScope.user!= null}"> <c:if test="${sessionScope.user!= null}">
<a href="/computerList">电脑选购</a> <a href="/computer?action=list">电脑选购</a>
<c:if test="${sessionScope.user.role == 'user'}"> <c:if test="${sessionScope.user.role == 'user'}">
<a href="/cart?action=list">我的购物车</a> <a href="/cart?action=list">我的购物车</a>
<a href="/orders?action=my">我的订单</a> <a href="/orders?action=my">我的订单</a>