• Home
  • About
    • Jiwon Jeong photo

      Jiwon Jeong

      끊임없이 배우며 성장하는 엔지니어

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
    • All Categories
  • Projects

MVC 내 JSP 렌더링

21 Jun 2021

Reading time ~1 minute

MVC 내 JSP 렌더링 방법

  • 예약확인 버튼을 누르면 해당 페이지로 가고싶다.
<a href="bookinglogin" class="btn_my"> <span class="viewReservation" title="예약확인">예약확인</span> </a>
  • 처음에는 그냥 views 폴더내에 bookinglogin.jsp 파일로하고 a href = ./bookinglogin.jsp 이런식으로 넣어서 구현했는데 404에러가 뜸

해결방법

  • ProductController.java 파일에 다음과 같이 해당 jsp 매핑정보를 입력해줘야함
	@GetMapping(path="/bookinglogin")
	public String bookinglogin() {
		
		return "bookinglogin";
	}
  • 상품을 클릭하면 해당 상품의 productId를 파라미터로 해서 detail페이지로 넘어가고싶다
  • 처음에는 아래와 같이 구현했더니 css를 불러오지 못하였음(아마 다른 정적 파일들도 못가져오지 않았을까 싶은데..)
    • http://localhost:8080/reservationProject/detail/50
    • Controller에는 아래와 같이 구현
      @GetMapping(path="/detail/{productId}")
      public String detail(@PathVariable(name="productId") int productId, ModelMap model) {
          List<ProductDetail> productDetails = detailService.getProductDetail(productId);
          model.addAttribute("productDetails", productDetails);
          return "detail";
      }
    

해결방법

  • Controller @PathVariable로 가져오지 말고, uri 식별자를 이용하여 다음과 같이 불러오도록 하니 잘 동작하였음
  • http://localhost:8080/reservationProject/detail?productId=50
    • Controller에는 아래와 같이 구현
      @GetMapping(path="/detail")
      public String detail(@RequestParam(name="productId") int productId, ModelMap model) {
          List<ProductDetail> productDetails = detailService.getProductDetail(productId);
          model.addAttribute("productDetails", productDetails);
          return "detail";
      }
    


개발일기 Share Tweet +1