프로젝트 코드리뷰 (1) - api비교 (검색/상품상세)

씩씩한 조약돌·2023년 5월 30일
0

코드 기록🤓

목록 보기
15/31

프로젝트를 하면서 계속해서 궁금했던 JSP와 react에서의 데이터 불러오는 api의 차이점과 @RequestParam과 @PathVariable의 차이점을 알아보고자 작성하는 글

1. 1차프로젝트 때 사용한 코드

1차프로젝트

  • 전자도서관 웹사이트 구축
  • 기술스택 : spring, jsp

1) 공지사항 리스트 검색

//게시글검색
	@RequestMapping(value = "/notice/search", method = RequestMethod.GET)
	   public String searchadmin(Model model, PageDTO pv, @RequestParam("option") String option,
	         @RequestParam("searchword") String searchword) {
		
	      List<NoticeDTO> dtos = null;
		  
	      // 페이지네이션을 위한 결과 개수 가져오기
	      int totalCount = userNoticeService.searchCountProcess(option, searchword);
	      model.addAttribute("totalCount", totalCount);
	      
	      if (totalCount >= 1) {
	         if (pv.getCurrentPage() == 0) 
	            pv.setCurrentPage(1);
	         this.pdto = new PageDTO(pv.getCurrentPage(), totalCount, searchword, option);
	         model.addAttribute("searchPv", this.pdto); //검색결과에 맞춘 pv 추가
	         model.addAttribute("Number", pdto.getNumber()); // 번호 추가
	          
		      dtos = userNoticeService.searchListProcess(this.pdto);
		      model.addAttribute("aList", dtos); //검색 결과 리스트 추가

	      }
	      model.addAttribute("query",searchword); //검색어 
	      model.addAttribute("option",option); //검색 옵션
	      return "notice/notice";
	   }	

2) 공지사항 게시글 조회

//공지사항 게시글 보기
	@RequestMapping("/notice/info")
	public ModelAndView viewExcute(@ModelAttribute("pv") PageDTO pv, int num, ModelAndView mav) {
		NoticeDTO bdto = userNoticeService.contentProcess(num);
		
		mav.addObject("dto", bdto); //게시글내용
		mav.addObject("pn", userNoticeService.preNextProcess(num)); //이전글,다음글
		mav.addObject("currentPage", pv.getCurrentPage());
		mav.setViewName("notice/view");
		return mav;
	}	

2. 2차프로젝트 때 사용한 코드

2차프로젝트

  • 캠핑장 예약, 캠핑용품 쇼핑 웹사이트 구축
  • 기술스택 : spring boot, react

1) 관리자페이지 - 상품 검색

//전체 캠핑용품 조회 + 검색
	@GetMapping("/admin/prod/prodlist/{currentPage}")
	public Map<String, Object> selectAllUser(@RequestParam ("startDate") String startDate,@RequestParam ("endDate") String endDate, @RequestParam ("state") String state, @RequestParam ("category")String category, @RequestParam  ("searchKey")String searchKey, @RequestParam  ("searchWord")String searchWord, PageDTO pv, @PathVariable("currentPage") int currentPage) {
		Map<String, Object> map = new  HashMap<>();

		Map<String, String> search = new HashMap<String, String>();
		search.put("startDate", startDate);
		search.put("endDate", endDate);
		search.put("state", state);
		search.put("category", category);
		search.put("searchKey", searchKey);
		search.put("searchWord", searchWord);

		int totalRecord = prodService.searchProdCountProcess(search);
		System.out.println("totalRecord : "+totalRecord);

		if(totalRecord>=1) {
			if(pv.getCurrentPage()==0)
				this.currentPage=currentPage;
			else
				this.currentPage = pv.getCurrentPage();

			this.pdto = new PageDTO(this.currentPage, totalRecord, startDate, endDate, state, category, searchKey, searchWord);
			System.out.println("currentPage Check : "+currentPage);

			map.put("prodList", prodService.searchProdListProcess(this.pdto));
			map.put("pv", this.pdto);
		}

		return map; 
	}
  • @PathVariable("currentPage") int currentPage과 @RequestParam("searchWord") String searchWord을 같이 사용할 수 있음!

2) 관리자페이지 - 상품 상세

	//상품상세
	@GetMapping("/admin/prod/detail/{prodKeyNum}")
	public ProdDTO viewExcute(@PathVariable("prodKeyNum") String prodKeyNum) {
		return prodService.detailProdProcess(prodKeyNum);
	}

3) 정리

  • param(/:), querystring(?,&)
  • param은 특정한 1개를 조회할 때 사용
    querystring은 조건이 여러개일 때, 검색할때, 정렬할 때 사용하면 편한 것 같다.

profile
씩씩하게 공부중 (22.11~)

0개의 댓글