创建Servlet
单击“”图标链接,发送“/findOrderById?id=6f591522-7a2a-4a31-899d-ef1181c72f5f&type=admin”,由FindOrderByIdServlet处理
/**
* 通过id查询订单
* @author admin
*
*/
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<OrderItem> items = oidao.findOrderItemByOrder(order);
order.setOrderItems(items);
} catch (SQLException e) {
e.printStackTrace();
}
return order;
}
编写DAO层方法
OrderDao
/**
* 根据id查找订单信息
* @param id
* @return
* @throws SQLException
*/
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<Order>() {
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<OrderItem> 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<List<OrderItem>>() {
public List<OrderItem> handle(ResultSet rs) throws SQLException {
List<OrderItem> items = new ArrayList<OrderItem>();
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"%>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK href="${pageContext.request.contextPath}/admin/css/Style.css" type="text/css" rel="stylesheet">
<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
</HEAD>
<body>
<table cellSpacing="1" cellPadding="5" width="100%" align="center"
bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4" height="26">
<strong>
订单详细信息
</strong>
</td>
</tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">订单编号:</td>
<td class="ta_01" bgColor="#ffffff">${order.id}</td>
<td align="center" bgColor="#f5fafe" class="ta_01">所属用户:</td>
<td class="ta_01" bgColor="#ffffff">${order.user.username }</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">收件人:</td>
<td class="ta_01" bgColor="#ffffff">${order.receiverName }</td>
<td align="center" bgColor="#f5fafe" class="ta_01">联系电话:</td>
<td class="ta_01" bgColor="#ffffff">${order.receiverPhone }</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">送货地址:</td>
<td class="ta_01" bgColor="#ffffff">${order.receiverAddress}</td>
<td align="center" bgColor="#f5fafe" class="ta_01">总价:</td>
<td class="ta_01" bgColor="#ffffff">${order.money }</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">下单时间:</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">${order.ordertime}</td>
</tr>
<TR>
<TD class="ta_01" align="center" bgColor="#f5fafe">商品信息</TD>
<TD class="ta_01" bgColor="#ffffff" colSpan="3">
<table cellspacing="0" cellpadding="1" rules="all" bordercolor="gray" border="1" id="DataGrid1"
style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
<tr style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">
<td align="center" width="7%">序号</td>
<td width="8%" align="center">商品</td>
<td align="center" width="18%">商品编号</td>
<td align="center" width="10%">商品名称</td>
<td align="center" width="10%">商品价格</td>
<td width="7%" align="center">购买数量</td>
<td width="7%" align="center">商品类别</td>
<td width="31%" align="center">商品描述</td>
</tr>
<c:forEach items="${order.orderItems}" var="item" varStatus="vs">
<tr style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #eeeeee">
<td align="center" width="7%">${vs.count }</td>
<td width="8%" align="center">
<img src="${pageContext.request.contextPath}${item.p.imgurl}" width="50" height="50">
</td>
<td align="center" width="18%">${item.p.id }</td>
<td align="center" width="10%">${item.p.name }</td>
<td align="center" width="10%">${item.p.price }</td>
<td width="7%" align="center">${item.buynum }</td>
<td width="7%" align="center">${item.p.category }</td>
<td width="31%" align="center">${item.p.description}</td>
</tr>
</c:forEach>
</table>
</TD>
</TR>
<TR>
<td align="center" colSpan="4" class="sep1"><img
src="${pageContext.request.contextPath}/admin/images/shim.gif">
</td>
</TR>
<TR>
<td class="ta_01" style="WIDTH: 100%" align="right" bgColor="#f5fafe" colSpan="4">
<FONT face="宋体"> </FONT>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回" />
<span id="Label1"></span>
</td>
</TR>
</table>
</body>
</HTML>
|