实现查看订单详情功能

实现查看订单详情功能,第1张

实现查看订单详情功能

创建Servlet

单击“”图标链接,发送“/findOrderById?id=6f591522-7a2a-4a31-899d-ef1181c72f5f&type=admin”,由FindOrderByIdServlet处理

public class FindOrderByIdServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.获取用户类型
		String type=request.getParameter("type");		
		//2.得到要查询的订单的id
		String id = request.getParameter("id");
		//3.根据id查找订单
		OrderService service = new OrderService();
		Order order = service.findOrderById(id);
        //4.将查询出的订单信息添加到request作用域中
		request.setAttribute("order", order);
		//5.如果用户类型不为null,则请求转发到view.jsp页面,否则转发到orderInfo.jsp页面
		if(type!=null){
			request.getRequestDispatcher("/admin/orders/view.jsp").forward(request, response);
			return;
		}
		request.getRequestDispatcher("/client/orderInfo.jsp").forward(request, response);
	}

}
编写Service层方法
// 根据id查找订单
public Order findOrderById(String id) {
	Order order = null;
	try {
		order = odao.findOrderById(id);
		List items = oidao.findOrderItemByOrder(order);
		order.setOrderItems(items);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return order;
}
编写DAO层方法

OrderDao

public Order findOrderById(String id) throws SQLException {
	String sql = "select * from orders,user "
			+ "where orders.user_id=user.id and orders.id=?";
	QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
	return runner.query(sql, new ResultSetHandler() {
		public Order handle(ResultSet rs) throws SQLException {
			Order order = new Order();
			while (rs.next()) {
				order.setId(rs.getString("orders.id"));
				order.setMoney(rs.getDouble("orders.money"));
				order.setOrdertime(rs.getDate("orders.ordertime"));
				order.setPaystate(rs.getInt("orders.paystate"));
				order.setReceiverAddress(rs.getString("orders.receiverAddress"));
				order.setReceiverName(rs.getString("orders.receiverName"));
				order.setReceiverPhone(rs.getString("orders.receiverPhone"));

				User user = new User();
				user.setId(rs.getInt("user.id"));
				user.setEmail(rs.getString("user.email"));
				user.setGender(rs.getString("user.gender"));
				user.setActiveCode(rs.getString("user.activecode"));
				user.setIntroduce(rs.getString("user.introduce"));
				user.setPassword(rs.getString("user.password"));
				user.setRegistTime(rs.getDate("user.registtime"));
				user.setRole(rs.getString("user.role"));
				user.setState(rs.getInt("user.state"));
				user.setTelephone(rs.getString("user.telephone"));
				user.setUsername(rs.getString("user.username"));
				order.setUser(user);
			}
			return order;
		}
	}, id);
}

OrderItemDao

// 根据订单查找订单项.并将订单项中商品查找到。
public List findOrderItemByOrder(final Order order)
		throws SQLException {
	String sql = "select * from orderItem,products "
			+ "where products.id=orderItem.product_id and order_id=?";

	QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());

	return runner.query(sql, new ResultSetHandler>() {
		public List handle(ResultSet rs) throws SQLException {

			List items = new ArrayList();
			while (rs.next()) {
				OrderItem item = new OrderItem();

				item.setOrder(order);
				item.setBuynum(rs.getInt("buynum"));

				Product p = new Product();
				p.setCategory(rs.getString("category"));
				p.setId(rs.getString("id"));
				p.setDescription(rs.getString("description"));
				p.setImgurl(rs.getString("imgurl"));
				p.setName(rs.getString("name"));
				p.setPnum(rs.getInt("pnum"));
				p.setPrice(rs.getDouble("price"));
				item.setP(p);

				items.add(item);
			}

			return items;
		}
	}, order.getId());
}
创建显示页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


	
	
	
	


	
		
			
				
					订单详细信息
				
			
		
		
			订单编号:
			${order.id}
			所属用户:
			${order.user.username }
		
		
			收件人:
			${order.receiverName }
			联系电话:
			${order.receiverPhone }
		
		
			送货地址:
			${order.receiverAddress}
			总价:
			${order.money }
		
		
			下单时间:
			${order.ordertime}
		
		
			商品信息
			
				
					
						序号
						商品
						商品编号
						商品名称
						商品价格
						购买数量
						商品类别
						商品描述
					
					
						
							${vs.count }
							
								
							
							${item.p.id }
							${item.p.name }
							${item.p.price }
							${item.buynum }
							${item.p.category }
							${item.p.description}
						
					
				
			
		
		
			
			
		
		
			
				       
				 
				
			
		
	

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5681898.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存