Spring

Spring 첨부파일 띄우기

테라시아 2025. 1. 4. 19:45

☆ Code

 

★ AttachFileMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 연결할 인터페이스 정보를 namespace에 기술 -->
<mapper namespace="com.board.mapper.AttachFileMapper">
	<insert id="insert">
		INSERT INTO TBL_ATTACH(FILENAME, UUID, UPLOADPATH, IMAGE, BNO)
		VALUES (#{fileName}, #{uuid}, #{uploadPath}, #{image}, #{bno})
	</insert>
	<select id="findByBno" resultType="attachFileVO">
		SELECT * FROM TBL_ATTACH WHERE BNO = #{bno}
	</select>
</mapper>

 

★ read.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Read Article</title>
</head>
<body>
	<!-- 
	<h1 th:text="${vo.title}"></h1>
	<hr>
	<p th:text="${vo.content}"></p>
	<p>글쓴이 : [[${vo.writer}]]</p>
	<p>등록일 : [[${vo.regdate}]]</p>
	<p>수정일 : [[${vo.updatedate}]]</p>
	-->
	<form action="/board/remove">
		<table border="1">
			<tr><td>제목</td><td th:text="${vo.title}"></td></tr>
			<tr><td>내용</td><td th:text="${vo.content}"></td></tr>
			<tr><td>저자</td><td th:text="${vo.writer}"></td></tr>
			<tr><td>등록</td><td th:text="${vo.regdate}"></td></tr>
			<tr><td>수정</td><td th:text="${vo.updatedate}"></td></tr>	
		</table>
		<div class="uploadResult">
			<h3>첨부파일</h3>
			<ul></ul>
		</div>
		<input type="hidden" name="bno" th:field="${vo.bno}">
		<button>삭제하기</button>
		<input type="button" onclick="goUpdate()" value="수정하기"> 
	</form>
</body>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script th:inline="javascript">
	function goUpdate(){
		let bno = [[${vo.bno}]];
		location.href = "/board/modify?bno=" + bno;
	}

	var title = [[${vo.bno}]];
	console.log(title);
	// alert(title);
	
	let bnoValue = [[${vo.bno}]];
	let ul = $(".uploadResult ul");
	
	$.getJSON("/board/getAttachList", { bno: bnoValue }, function(arr){
		console.log(arr);
		str = "";
		
		$(arr).each(function(i, attach) {
			newName = attach.fileName.substring(attach.fileName.indexOf("_")+1);
			str += "<li>" + newName + "</li>";
		});
		
		ul.html(str);
	});
</script>
</html>

'Spring' 카테고리의 다른 글

Spring 파일 업로드 심화  (1) 2024.12.31
Spring 파일 업로드  (0) 2024.12.30
Spring 미디어쿼리를 통한 반응형 페이지 추가  (1) 2024.12.28
Spring 글 수정 로직  (0) 2024.12.27
Spring 게시판 글 작성, 삭제 로직  (0) 2024.12.26