project code menu_image(mainpage2)

별하·2024년 7월 9일

프로젝트2

목록 보기
8/10

- store_menu테이블에 이미지등록시 store_img경로 테이블에도 저장되어 경로를 통해 mainpage2 뷰에 보이는 code

1. controller

// mainPage2 요청 처리===================================
	@RequestMapping("mainPage2")
	public String mainPage2(@RequestParam(required=false) String keyword, 
	                        @RequestParam(required=false) String searchType,
	                        @RequestParam(required=false) String category,
	                        HttpSession session, Model model) {   
		//String user = (String) session.getAttribute("userId");
		//String store = (String) session.getAttribute("storeId");
	    
	    Map<String, Object> params = new HashMap<>();
	    String key = (keyword != null) ? keyword : "null";
	    String search = (searchType != null) ? searchType : "null";
	    String cat = (category != null) ? category : "null";

	    params.put("keyword", key);
	    params.put("searchType", search);
	    params.put("category", cat);

	    List<MainMapDTO> storeList = ms.getStoreInfo(params);
	    //List<MainMapDTO> storeListBt = ms.getStoreInfo(params);
	   
	    if (storeList == null) {
	        storeList = new ArrayList<>();
	    }

	   // if (imgList == null) {
	     //   imgList = new ArrayList<>();
	   // }
	    
	    List<MainImgDTO> storeImgList = new ArrayList<>();
	    for(MainMapDTO storeInfo : storeList) {
	    	List<MainImgDTO> storeImage = ms.getStoreImage(storeInfo.getStore_id());
	    	//storeImgList.add(storeImage);
	    	  if (!storeImage.isEmpty()) {
	              storeImgList.add(storeImage.get(0));
	              System.out.println("image path: "+storeImage.get(0).getStore_img_root());
	          } else {
	              storeImgList.add(null); // 이미지가 없는 경우
	          }
	    }
	    
	    List<MainImgDTO> storeSmallImgList = new ArrayList<>();
	    for(MainMapDTO storeInfo : storeList) {
	    	List<MainImgDTO> storeSmallImage = ms.getStoreSmallImage(storeInfo.getStore_id());
	    	  if (!storeSmallImage.isEmpty()) {
	    		  int maxImages = Math.min(4, storeSmallImage.size());
	    		  for(int i = 0; i < maxImages; i++) {
	    			  storeSmallImgList.add(storeSmallImage.get(i));
	              System.out.println("image path: "+storeSmallImage.get(i).getStore_img_root());
	    		  }
	          } else {
	        	  storeSmallImage.add(null); // 이미지가 없는 경우
	          }
	    }

	    model.addAttribute("storeList", storeList);
	    model.addAttribute("storeListSize", storeList.size());
	    //model.addAttribute("imgList", imgList);
	    model.addAttribute("storeImgList", storeImgList);
	    model.addAttribute("storeSmallImgList", storeSmallImgList);
	    
	    model.addAttribute("keyword", keyword);
	    model.addAttribute("searchType", searchType);
	    model.addAttribute("category", category);

	    return "main/mainPage2";
	}

2. serviceimpl

public List<MainImgDTO> getStoreImage(String storeId) {
		return mapper.getStoreImage(storeId);
	}
	public List<MainImgDTO> getStoreSmallImage(String storeId) {
		return mapper.getStoreSmallImage(storeId);
	}

3. mapper

public List<MainImgDTO> getStoreImage(String storeId);
public List<MainImgDTO> getStoreSmallImage(String storeId);

4. mapper.xml

<select id="getStoreImage" resultMap="mainImg">
    SELECT * FROM store_img 
    WHERE store_id = #{storeId} 
    AND store_img_main = 2 
    AND ROWNUM = 1
    ORDER BY store_img_root DESC 
</select>

<select id="getStoreSmallImage" resultMap="mainImg">
    SELECT * FROM store_img 
    WHERE store_id = #{storeId} 
    AND store_img_main = 2 
    ORDER BY store_img_root DESC 
</select>

5. mainpage2.jsp

<c:choose>
  <c:when test="${not empty storeList}">
    <c:forEach var="store" items="${storeList}" varStatus="status">
      <div class="menu-detail">
        <div class="detail-container-left">

          <div class="menu-detail-imgBig">
          <c:if test="${not empty storeImgList[status.index]}">
          	<img class="imgBig" src="${path}/main/download?fileName=${storeImgList[status.index].store_img_root}" alt="Store Image">
          </c:if>
         </div>
         <div class="menu-detail-imgSmall">
          <c:forEach var="smallImg" items="${storeSmallImgList}" begin="1" end="3">
         	 <c:if test="${not empty smallImg}">
          		<div class="imgSmall1${status.index }">
          			<img class="imgSmall" src="${path}/main/download?fileName=${smallImg.store_img_root}" alt="Store Menu Image">
          		</div>
          	 </c:if>
          </c:forEach>
         </div>

0개의 댓글