본문 바로가기
CLASS/SPRING,JSTL

#4-3 / gallery board select-list (spring + I/O + database)

by hingu 2024. 7. 11.

👀 list

 

- dao 수정 : 첨부파일도 가지고오려고..! (썸네일 타입 board list)

package shop;

import java.util.ArrayList;
import java.util.Arrays;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class gallery_dao {
	int gidx;
	String gwriter,gsubject,gtext;
	String gorifile,gfile;
	String gindate;
	
	//list 페이지에서 필요한것만! 
      //=> 첨부하지 않을경우,첨부할 경우 배열 length가 달라질 수 있기 때문에 아예 분리해서 받는게 좋다!
	public ArrayList<Object> lists(){ 
		ArrayList<Object> al = new ArrayList<Object>();
		
		al.add(getGidx());
		al.add(getGwriter());
		al.add(getGsubject());
		al.add(getGindate());
		
		//추가사항 - 첨부파일 이미지
		al.add(getGfile());
		
		return al;
	}
	
	//view 페이지는 전부 다 필요함
	public ArrayList<Object> views(){
		ArrayList<Object> al = new ArrayList<Object>();
		
		al.add(getGidx());
		al.add(getGwriter());
		al.add(getGsubject());
		al.add(getGtext());
		al.add(getGorifile());
		al.add(getGfile());
		al.add(getGindate());
		
		return al;
	}
}

 

 

- shop_main2.java - @GetMapping("/gallery.do")

: gallery_select 로 db정보, dao 인자값으로 던진 후 2차배열로 return받아 

  Model을 이용해 jstl로 2차배열 던짐

//gallery - list 파트
@GetMapping("/gallery.do")
public void gallery_list( 
		Model m , @ModelAttribute("ga") gallery_dao dao, HttpServletRequest req
    ) throws Exception{ //model이용해 jstl로 보낼거임
    
    gallery_select gs = new gallery_select();
    ArrayList<ArrayList<Object>> resultarr = gs.file_list(dbInfo, dao);

    String url = req.getServletContext().getRealPath("/upload/"); //웹경로

    m.addAttribute("size",resultarr.size());
    m.addAttribute("resultarr",resultarr);
    m.addAttribute("url",url);
}

 

- gallery_select.java :  shop_main2.java 에서 호출 , dao에서 받아온 1차배열을 2차배열에 담아 return

package shop;

//~ import

public class gallery_select{
	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	ArrayList<ArrayList<Object>> all = null;
	
	//게시물 리스트
	public ArrayList<ArrayList<Object>> file_list(
			BasicDataSource dbinfo,gallery_dao dao) 
		throws Exception{
		
		gallery_dao gd = new gallery_dao(); //dao load
		this.con = dbinfo.getConnection();

		try {
			String sql = "select gidx,gsubject,gwriter,gindate,gfile from gallery"
					+ " order by gidx desc";
			this.ps = this.con.prepareStatement(sql);
			this.rs = this.ps.executeQuery();
			
			this.all = new ArrayList<ArrayList<Object>>();
			while(this.rs.next()) {
				gd.setGidx(Integer.parseInt(this.rs.getString(1)));
				gd.setGsubject(this.rs.getString(2));
				gd.setGwriter(this.rs.getString(3));
				gd.setGindate(this.rs.getString(4));
				gd.setGfile(this.rs.getString(5));
				
				this.all.add(gd.lists());
			}
				
		}catch(Exception e) {
			System.out.println("db오류!");
		}finally {
			this.rs.close();
			this.ps.close();
			this.con.close();
		}
		
		return this.all;
	}
	
	//게시물 view
}

 

- gallery.jsp 생성

 

- jstl 조건문 사용 : 첨부파일이 있을경우, 없을경우 썸네일 노출

- servelt을 사용하기 위해서는 functions 엔진을 로드해야함

fn : contains,split,startWidth,join,length

엔진prefix값:split(분리할문자,'분리기준문자'); => 원시배열로 구성됨 []

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="gl" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- servelt을 사용하기 위해서는 functions 엔진을 로드해야함 -->
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>gallery 게시판 list</title>
</head>
<body>
	<p>갤러리 게시판</p>
		<table border="1" cellpadding="0" cellspacing="0">
		<thead>
			<tr>
				<th>번호</th>
				<th>썸네일</th>
				<th>제목</th>
				<th>글쓴이</th>
				<th>등록일</th>
			</tr>	
		</thead>
		<tbody>
			<gl:forEach var="gallerydata" items="${resultarr}" varStatus="status">
				<tr>
					<td style="text-align:center;">${size-status.index}</td>
					<gl:if test="${gallerydata.get(4)==null}">
						<td>No Image</td>
					</gl:if>
					<gl:if test="${gallerydata.get(4)!=null}">
						<gl:set var="imgs" value="${fn:split(gallerydata.get(4),',')}"></gl:set>
						<td>
							<img src='.${url.split("webspring")[1]}${imgs[0]}' style="max-width : 100px;">
						</td>
					</gl:if>
					
					<td onclick="gallery_view('${gallerydata.get(0)}')">${gallerydata.get(2)}</td>
					<td>${gallerydata.get(1)}</td>
					<td>${gallerydata.get(3).split(" ")[0]}</td>   
				</tr>
			</gl:forEach>
		</tbody>
	</table><br>
	<a href="./gallery_write.jsp">글쓰기</a>
</body>

<script>
function gallery_view(gidx){
	location.href="./gallery_view.do?gidx="+gidx;
}
</script>


</html>

요렇게 list 출력

 

=> view로 보낼거임