BoardVO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@EqualsAndHashCode(exclude= {"title","author","regDate","contents","hit","attList"})
@AllArgsConstructor
@NoArgsConstructor
public class BoardVO {
private int boardid;
private String title;
private String author;
private java.sql.Date regDate;
private String contents;
private int hit;
private List<AttachVO> attList = new ArrayList<>();
}
|
cs |
AttachVO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@EqualsAndHashCode(exclude= {"title","author","regDate","contents","hit","attList"})
@AllArgsConstructor
@NoArgsConstructor
public class BoardVO {
private int boardid;
private String title;
private String author;
private java.sql.Date regDate;
private String contents;
private int hit;
private List<AttachVO> attList = new ArrayList<>();
}
|
cs |
BoardMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
|
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import com.ezen.spring.web.vo.AttachVO;
import com.ezen.spring.web.vo.BoardVO;
@Mapper
public interface BoardMapper {
void saveAttach(List<AttachVO> list);
void saveBoard(BoardVO board);
List<BoardVO> getList();
}
|
cs |
BoardMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?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">
<mapper namespace="com.ezen.spring.web.mapper.BoardMapper">
<insert id="saveBoard"
parameterType="com.ezen.spring.web.vo.BoardVO">
<selectKey keyProperty="num" resultType="integer" order="BEFORE">
SELECT BOARD_SEQ.NEXTVAL FROM DUAL
</selectKey>
-->
INSERT INTO board(boardid, title, contents, author)
VALUES(BOARD_SEQ.NEXTVAL, #{title,jdbcType=VARCHAR}, #{contents,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR})
</insert>
<update id="saveAttach" parameterType="list">
INSERT INTO attach (attachid,boardid , fname, fsize)
SELECT ATTACH_SEQ.NEXTVAL AS attachid, t.* FROM
(
<foreach collection="list" item="item" index="index" separator="union all">
SELECT (SELECT MAX(boardid) FROM board) AS boardid, #{item.fname}, #{item.fsize} FROM DUAL
</foreach>
) t
</update>
<select id="getList"
resultType="com.ezen.spring.web.vo.BoardVO">
SELECT * FROM board
</select>
</mapper>
|
cs |
BoardController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import java.io.File;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.ezen.spring.web.mapper.BoardMapper;
import com.ezen.spring.web.vo.AttachVO;
import com.ezen.spring.web.vo.BoardVO;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/board")
public class BoardController {
@Autowired
ResourceLoader resourceLoader;
@Autowired
private BoardMapper dao;
@GetMapping("/add")
public String getForm()
{
return "board/boardAdd";
}
@PostMapping("/add")
@ResponseBody
@Transactional
public String upload(@RequestParam("files")MultipartFile[] mfiles,
HttpServletRequest request,
BoardVO board)
{
ServletContext context = request.getServletContext();
String savePath = context.getRealPath("/WEB-INF/files");
List<AttachVO> list = new ArrayList<AttachVO>();
try {
for(int i=0;i<mfiles.length;i++) {
mfiles[i].transferTo(
new File(savePath+"/"+mfiles[i].getOriginalFilename()));
long fSize = mfiles[i].getSize();
String fName = mfiles[i].getOriginalFilename();
AttachVO att = new AttachVO();
att.setFname(fName);
att.setFsize(fSize);
list.add(att);
}
String msg = String.format("ํ์ผ(%d)๊ฐ ์ ์ฅ์ฑ๊ณต(์์ฑ์:%s)", mfiles.length,board.getAuthor().toString());
dao.saveBoard(board);
dao.saveAttach(list);
return msg;
} catch (Exception e) {
e.printStackTrace();
return "ํ์ผ ์ ์ฅ ์คํจ:";
}
}
@GetMapping("/list")
public String getList(Model m)
{
m.addAttribute("list", dao.getList());
return "board/boardList";
}
}
|
cs |
boardAdd.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ฒ์๊ธ ์ถ๊ฐ</title>
<style type="text/css">
@import url(//fonts.googleapis.com/earlyaccess/jejumyeongjo.css);
.jm-font{
font-family: 'Jeju Myeongjo', cursive;
color: orange;
font-size:1.5em;
}
.main-font{
font-family: 'Jeju Myeongjo', serif;
color: black;
font-size:1.5em;
}
.wrapper {
display: flex;
justify-content: center;
align-items: center;
}
main { width: fit-conent; margin:1em auto;}
main h1 { text-align: center; }
label { display:inline-block; margin-right:1em; width:3em;
background-color:rgb(248,242,24); text-align:center; padding:0.5em;
}
div { margin-top:0.5em; }
</style>
</head>
<body style="background-color: rgb(254,223,255);">
<span class="wrapper">
<main class="main-font">
<h1 class="jm-font">๊ฒ์๊ธ ์
๋ ฅ</h1>
<form action="/board/add" method="post" enctype="multipart/form-data">
<div>
<label for="title">์ ๋ชฉ</label>
<input id="title" type="text" name="title" value="">
</div>
<div>
<label for="author">์์ฑ์</label>
<input id="author" type="text" name="author" value="">
</div>
<div>
<label for="contents">๋ด์ฉ</label>
<textarea id="contents" name="contents" rows="5" cols="50"></textarea>
</div>
<div>
์ฒจ๋ถํ์ผ <input type="file" name="files" multiple="multiple"><br>
</div>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">์ ์ฅ</button>
</div>
</form>
</main>
</span>
</body>
</html>
|
cs |
boardList.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๊ฒ์๊ธ ๋ชฉ๋ก</title>
</head>
<body>
<c:forEach var="board" items="${list}">
${board.boardid} 
<a href="/board/detailBoard/${board.boardid}">${board.title}</a> 
${board.author} 
<br>
</c:forEach>
</body>
</html>
|
cs |
์คํ ๊ฒฐ๊ณผ :
๋๊ธ