use of com.worksmobile.Assignment.Service.Paging in project Assignment by WMPeople.
the class RestController method boardList.
/**
* 첫 화면으로, 사용자가 요청한 페이지에 해당하는 게시물을 보여줍니다.
* @param req pages 파라미터에 사용자가 요청한 페이지 번호가 있습니다.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public ModelAndView boardList(HttpServletRequest req) throws Exception {
// /(localhost:8080)페이지로 오면 처음에 표시할 페이지 (1 = 첫번째 페이지)
int currentPageNo = CURRENT_PAGE_NO;
// 페이지당 표시될 게시물 최대 갯수
int maxPost = MAX_POST;
if (// 게시물이 1개도없으면(=페이지가 생성이 안되었으면)이 아니라면 == 페이징이 생성되었다면
req.getParameter("pages") != null)
// pages에있는 string 타입 변수를 int형으로 바꾸어서 currentPageNo에 담는다.
currentPageNo = Integer.parseInt(req.getParameter("pages"));
// Paging.java에있는 currentPageNo, maxPost를 paging변수에 담는다.
Paging paging = new Paging(currentPageNo, maxPost);
int offset = (paging.getCurrentPageNo() - 1) * paging.getmaxPost();
// 현재 3페이지 이고, 그 페이지에 게시물이 10개가 있다면 offset값은 (3-1) * 10 = 20이 된다.
// BoardDTO에 있는 변수들을 ArrayList 타입의 배열로 둔 다음 이를 page라는 변수에 담는다.
ArrayList<BoardDTO> board = new ArrayList<BoardDTO>();
HashMap<String, Integer> params = new HashMap<String, Integer>();
params.put("offset", offset);
params.put("noOfRecords", paging.getmaxPost());
board = (ArrayList<BoardDTO>) boardMapper.articleList(params);
// writeService.java에 있는 articleList 함수를 이용하여 offset값과 maxPost값을 ArrayList 타입의 배열로 담고, 이 배열 자체를 page 변수에 담는다.
// 페이지를 표시하기 위해 전체 게시물 수를 파악하기 위한것
paging.setNumberOfRecords(boardMapper.articleGetCount());
paging.makePaging();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("board", board);
modelAndView.addObject("paging", paging);
modelAndView.setViewName("boardList");
return modelAndView;
}
Aggregations