(九)显示交易记录 &解决相对路径问题
UserServlet.java
package com.aff.bookstore.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.aff.bookstore.domain.User; import com.aff.bookstore.service.UserService; @WebServlet("/userServlet") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; private UserService userService = new UserService(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取username 请求参数的值 String username = request.getParameter("username"); // 调用UserService 的 getUser(username) 获取 User对象, 要求 trade 是被装配好的 , 而且 // 每一个trade 对象的items User user = userService.getUserWithTrades(username); // 把User 对象方法放reuest 中 if (user == null) { response.sendRedirect(request.getServletPath()+"errror-1.jsp"); return; } request.setAttribute("user", user); // 转发页面到/WEB-INF/pages/trades.jsp request.getRequestDispatcher("/WEB-INF/pages/trades.jsp").forward(request, response);; } }
users.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/commons/common.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <br><br> <form action="userServlet" method="post"> username: <input type="text" name="username"/> <input type="submit" value="Submit"> </form> </center> </body> </html>
trades.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/commons/common.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <br><br> <h3>User: ${user.username}</h3> <br><br> <table> <c:forEach items="${user.trades }" var="trade"> <tr> <td> <table border="1" cellpadding="10" cellspacing="0"> <tr> <td colspan="3">TradeTime:${trade.tradeTime }</td> </tr> <c:forEach items="${trade.items }" var="item"> <tr> <td>${item.book.title }</td> <td>${item.book.price }</td> <td>${item.quantity }</td> </tr> </c:forEach> </table> <br> </td> </tr> </c:forEach> </table> </center> </body> </html>
TradeDAOImpl.java
package com.aff.bookstore.dao.impl; import java.util.LinkedHashSet; import java.util.Set; import com.aff.bookstore.dao.TradeDAO; import com.aff.bookstore.domain.Trade; public class TradeDAOImpl extends BaseDAO<Trade> implements TradeDAO { @Override public void insert(Trade trade) { String sql = "insert into trade(userid,tradetime) values(?,?)"; long tradeId = insert(sql, trade.getUserId(),trade.getTradeTime()); trade.setTradeId((int)tradeId); } @Override public Set<Trade> getTradesWithUserId(Integer userId) { String sql = "select tradeId,userId, tradeTime from trade where userId = ? order by tradeTime desc"; return new LinkedHashSet<>(queryForList(sql, userId)); } }
效果如下
解决相对路径问题:
common.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <base href="${pageContext.request.scheme }://${pageContext.request.serverName }:${pageContext.request.serverPort }${pageContext.request.contextPath}/">
在需要的jsp中添加<%@ include file="/commons/common.jsp" %>
如下:empty.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/commons/common.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>您的购物车为空</h4> <a href="index.jsp">继续购物</a> </body> </html>
整个目录
All that work will definitely pay off