@RequestMapping(value = "/admin/addblogaction", method = RequestMethod.POST) public String addblogaction(@ModelAttribute("blog") Blog blog, ModelMap model) { // TODO how to get blog from url?? Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name = auth.getName(); logger.debug("name: " + name); blog.setInsertDate(new Timestamp(System.currentTimeMillis())); blog.setAuthor(name); blogService.insertBlog(blog); ListResult<Blog> listResult = getBlogsByPage(1, 3); model.addAttribute("listOfBlogs", listResult.getFetchedList()); model.addAttribute("totalPage", listResult.getTotalPage()); model.addAttribute("currentPage", listResult.getCurrentPage()); return "admin"; }
private ListResult<Blog> getBlogsByPage(int pageNum, int pageSize) { // List<Blog> listOfBlogs = blogService.listAllBlog(); int currentPage, totalPage, totalCount; // 1. get total count // 2. totalPage = total count / 3 + ((total count % 3) == 0) ? 0 : 1 // 3. currentPage = page, currentPage -> items?? // 4. select blog_id from blog order by insertDate desc => list // 5. list ( [currentPage-1)*3 + 1, (currentPage)*3 ] ) => (set of blog_id) // 6. select * from blog where blog_id in (set of blog_id) |||| in (?) a = ? List<Integer> orderedIdList = blogService.listAllBlogIds(); for (int id : orderedIdList) { logger.debug("order id: " + id); } totalCount = orderedIdList.size(); logger.info("total count: " + totalCount); totalPage = totalCount / pageSize + (((totalCount % pageSize) == 0) ? 0 : 1); currentPage = pageNum; List<Integer> selectedIdList = new ArrayList<Integer>(); for (int k = ((currentPage - 1) * 3); k < (currentPage) * 3; ++k) { selectedIdList.add(orderedIdList.get(k)); logger.debug("added id: " + orderedIdList.get(k)); } List<Blog> listOfBlogs = blogService.getSelectedBlogs(selectedIdList); for (Blog b : listOfBlogs) { logger.debug("returned id: " + b.getBlogId()); } logger.info("totalPage:" + totalPage); logger.info("currentPage: " + currentPage); logger.info("list of blogs size: " + listOfBlogs.size()); ListResult<Blog> listResult = new ListResult<>(); listResult.setTotalPage(totalPage); listResult.setFetchedList(listOfBlogs); listResult.setCurrentPage(currentPage); return listResult; }