前言
看视频跟着老师敲完代码,对前后端交互有了很大的认识。在视频结尾,我们老师要求自己下来实现<我的订单>功能。通过点击我的订单,可查询自己的预订详情。如下图:
在这里,通过csdn发现了一个博主写的很好,于是便参考他的博文编写了此功能。
后端代码:
【servlet】
UserServlet.java:
//获取 我的订单 protected void findMyReserve(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session中获取用户 HttpSession session = request.getSession(); //2.获取当前登录的用户 user User user = (User) session.getAttribute("user"); if (user == null) { //用户未登录 writeValue(null,response); return; } //获取并处理浏览器请求属性 String currentPageStr = request.getParameter("currentPage");//当前页码 String pageSizeStr = request.getParameter("pageSize");//每页显示条数 int currentPage = this.parseInt(currentPageStr, 1); int pageSize = this.parseInt(pageSizeStr, 8); //获取service,根据uid查询rid,再根据rid查询详情数据 PageBean<Hotel> hotelPageBean = service.reservePageQuery(user.getUid(), currentPage, pageSize); // 3.将数据回写至浏览器 writeValue(hotelPageBean, response); }
【service】
UserServiceImpl:@Override public PageBean<Hotel> reservePageQuery(int uid, int currentPage, int pageSize) { //创建PageBean<Route>对象 PageBean<Hotel> hotelPageBean = new PageBean<>(); //查询总记录数totalCount int totalCount = reserveDao.findCountByUid(uid); if (totalCount == 0) {// 没有查到收藏记录 return hotelPageBean; } //计算起始记录数start,计算总页数totalPage int start = (currentPage - 1) * pageSize; int totalPage = (totalCount % pageSize == 0) ? (totalCount / pageSize) : (totalCount / pageSize + 1); // 分页查询rid列表(封装在MyFavorite类中) List<MyReserve> pageReserveList = reserveDao.findByUidAndPage(uid, start, pageSize); // 创新一个空的List<Route>集合 List<Hotel> hotelList = new ArrayList<>(); // 遍历pageFavoriteList组装routeList for (MyReserve myReserve : pageReserveList) { // 根据其rid属性利用routeDao查route对象 Hotel hotel = hotelDao.findOne(myReserve.getHid()); // 向routeList中追加route属性 hotelList.add(hotel); } // 并组装PageBean<Route>对象 hotelPageBean.setCurrentPage(currentPage); // 设置当前页码 hotelPageBean.setPageSize(pageSize); // 设置每页显示条数 hotelPageBean.setTotalPage(totalPage); // 设置总页数 hotelPageBean.setTotalCount(totalCount); // 查询并设置总记录数 hotelPageBean.setList(hotelList); return hotelPageBean; }
【Dao】
ReserveDaoImpl:
//查询用户uid的收藏总数 @Override public int findCountByUid(int uid) { //定义sql String sql = "SELECT COUNT(*) FROM tab_reserve WHERE uid = ?"; //执行sql return template.queryForObject(sql, Integer.class, uid); } //分页查询用户uid的收藏记录 @Override public List<MyReserve> findByUidAndPage(int uid, int start, int pageSize) { String sql = "SELECT * FROM tab_reserve WHERE uid = ? LIMIT ? , ?"; return template.query(sql, new BeanPropertyRowMapper<MyReserve>(MyReserve.class), uid, start, pageSize); }
看看“学java的秀琴”对前后端交互的理解:
一、接受html传来的参数——request
//方式一.接受参数 String currentPageStr = request.getParameter("currentPage"); String pageSizeStr = request.getParameter("pageSize"); String cidStr = request.getParameter("cid");
//方式二.从session中获取登录用户 Object user = request.getSession().getAttribute("user");
//方式三.获取用户名和密码数据 Map<String, String[]> map = request.getParameterMap(); //2.封装User对象 User user = new User(); try { BeanUtils.populate(user, map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //3.调用Service查询 // UserService service = new UserServiceImpl(); User u = service.login(user);
---------------------------------------------------------------------------------------
二、返回html传来的数据——response
//方式一.响应数据 ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(), info);
//方式二.响应数据 writeValue(user, response);
//方式三.响应数据 response.setContentType("text/html;charset=utf-8"); response.getWriter().write(msg);