功能
对已经支付的订单可以停止删除操作
编辑页面代码
- <script type="text/javascript">
- //删除订单
- function o_del() {
- var msg = "您确定要删除该订单吗?";
- if (confirm(msg)==true){
- return true;
- }else{
- return false;
- }
- }
- </script>
- <!-- 已支付,弹出提示框由操作人确认 -->
- <c:if test="${order.paystate!=0 }">
- <a href="${pageContext.request.contextPath}/delOrderById?id=${order.id}&type=admin" onclick="javascript:return o_del()">
-
- </a>
- </c:if>
- <!-- 未支付 ,不能删除-->
- <c:if test="${order.paystate==0 }">
- <a href="javascript:alert('不能删除未支付订单')">
-
- </a>
- </c:if>
复制代码 创建Servlet
- /**
- * 删除订单
- * @author admin
- *
- */
- public class DelOrderByIdServlet extends HttpServlet {
- private static final long serialVersionUID = -742965707205621644L;
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // 订单id
- String id = request.getParameter("id");
- // 已支付的订单带有type值为client的参数
- String type = request.getParameter("type");
- OrderService service = new OrderService();
- if (type != null && type.trim().length() > 0) {
- service.delOrderById(id);
- if ("admin".equals(type)) {
- request.getRequestDispatcher("/findOrders").forward(request, response);
- return;
- }
- } else {
- // 调用service层方法删除相应订单
- service.delOrderByIdWithClient(id);
- }
- //response.sendRedirect(request.getContextPath() + "/client/delOrderSuccess.jsp");
- request.getRequestDispatcher("/findOrderByUser").forward(request, response);
- return;
- }
- }
复制代码 编写Service层方法
- //根据id删除订单 管理员删除订单
- public void delOrderById(String id) {
- try {
- DataSourceUtils.startTransaction();//开启事务
- oidao.delOrderItems(id);
- odao.delOrderById(id);
- } catch (SQLException e) {
- e.printStackTrace();
- try {
- DataSourceUtils.rollback();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }finally{
- try {
- DataSourceUtils.releaseAndCloseConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 编写DAO方法
OrderDao- /**
- * 根据id删除订单
- * @param id
- * @throws SQLException
- */
- public void delOrderById(String id) throws SQLException {
- String sql="delete from orders where id=?";
- QueryRunner runner = new QueryRunner();
- runner.update(DataSourceUtils.getConnection(),sql,id);
- }
复制代码 OrderItemDao- //根据订单id删除订单项
- public void delOrderItems(String id) throws SQLException {
- String sql="delete from orderItem where order_id=?";
-
- QueryRunner runner=new QueryRunner();
-
- runner.update(DataSourceUtils.getConnection(),sql,id);
- }
复制代码 |