ํฐ์คํ ๋ฆฌ ๋ทฐ
Controller
PostsController
//๊ฒ์๊ธ ์์ฑ ํ์ด์ง personal-write.jsp ์์ฒญ
@RequestMapping(value="/{id}/personal-write", method=RequestMethod.GET)
public String personalWrite(@PathVariable String id, Model model, HttpSession session) throws Exception {
//id๋ก ํ์์ ๋ชจ๋ ์ ๋ณด ์กฐํ
UsersVO user = usersService.inquiryOfUserById(id);
//๋ก๊ทธ์ธํ ํ์ (=๋) ์ ๋ณด ๋ด๊ธฐ
Object object = session.getAttribute("login");
UsersVO loginUser = (UsersVO)object;
//๊ฐ์ธํ์ด์ง์ ์ ์ ๋ฒํธ ๊ฐ์ ธ์ค๊ธฐ
int userNo = user.getUserNo();
//๋ก๊ทธ์ธ ํ์ ์ ์ ๋ฒํธ ๊ฐ์ ธ์ค๊ธฐ
int loginUserNo = loginUser.getUserNo();
logger.info("ํ์ฌ ํ์ด์ง ์ฃผ์ธ์ ๋ฒํธ : " + userNo + ", ๋ก๊ทธ์ธ ์ ์ ์ ๋ฒํธ : " + loginUserNo);
//์ฌ์ฉ์ ์์ด๋๋ก ์ฌ์ฉ์ ๋ฒํธ(pk)๋ฅผ ์กฐํํด์ ๊ทธ ๋ฒํธ๋ก ๊ฒ์๋ฌผ ๊ฐ์ ธ์ค๊ธฐ
model.addAttribute("post", postsService.selectPostListById(id));
//ํ์์ ์ ๋ณด
model.addAttribute("user", user);
return "post/personal-write";
}
์ธ์คํ๊ทธ๋จ ์นํ์ด์ง์์๋ ๊ฒ์๊ธ์ ์ ๋ก๋ํ ์ ์๋๋ก ๋์ด์๋ค. ํ์คํ๊ทธ๋จ์์๋ ์ ๋ก๋ํ ์ ์๋๋ก ๊ฐ์ธ์ ์ผ๋ก ํ์ ๋ง๋ค์ด๋ณด์๋ค. ํ๋ก ํธ์๋ ๋ค๋ฃจ๋๊ฒ ์ต์์น ์์์ ๋ฐ์ ๋ฒํผ ๋๊ฐ๋ฅผ ๋ฐฐ์นํ๋๋ฐ ์ด๋ ค์์ ๋๊ผ๋ค. ๊ฒฐ๊ตญ ๋ฐฐ์น๋ ์์ ์ฌ์ง์ํ๋ก ์์ฑ ๋์์ง๋ง ๋ค๋ฅธ ๊ธฐ๋ฅ๋ค ๋ง๋ค๊ณ ๋ค์ ๊ฑด๋ค์ฌ๋ด์ผ๊ฒ ๋ค.
//๊ฒ์๊ธ ๋ฑ๋ก ์์ฒญ
@RequestMapping(value="/upload", method=RequestMethod.POST)
public String write(PostsVO post) throws Exception {
logger.info("/post/upload : POST์์ฒญ");
//ํ๋ก ํธ์์ ๊ฐ์ ธ์จ ๊ฒ์๊ธ ํ์ธ
logger.info("๊ฐ์ ธ์จ ๊ธ : " + post.toString());
//ํ์ผ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค.
MultipartFile mfile = post.getFile();
//post์ ๊ฐ ์ง์ ํด์ฃผ๊ธฐ
post.setFileName(mfile.getOriginalFilename()); //ํ์ผ ์ด๋ฆ ์ง์
post.setFileSize(mfile.getSize());
post.setFileContentType(mfile.getContentType());
post.setFileData(mfile.getBytes());
//๊ฒ์๊ธ ๋ด์ฉ ๋ค์ ํ์ธ
logger.info("๊ฒ์๊ธ : " + post.toString());
//๊ฒ์๊ธ ๋ฑ๋ก ์์ฒญ
postsService.insertPost(post);
//์ฌ์์ฒญ redirect : DB๋ฅผ ๊ฑฐ์ณ์ ๋์์ผํ๊ธฐ ๋๋ฌธ์
return "redirect:/post/list";
ํ๋ก ํธ์์ user_no, id, caption, file์ ๊ฐ์ ธ์จ๋ค. ํ์ผ ์ฒ๋ฆฌ๋ฅผ ํด์ค ํ DB์ ๋ฑ๋ก ์์ฒญ์ ํ๋ค. DB๋ฅผ ๊ฑฐ์ณ ๋์์ผํ๊ธฐ ๋๋ฌธ์ redirect๋ก ์ฌ์์ฒญํ๋ค.
Service
PostsService
//๊ฒ์๋ฌผ ๋ฑ๋ก ๊ธฐ๋ฅ(์ฌ์ง ์ฒจ๋ถํ์ผ ํฌํจ)
@Override
public void insertPost(PostsVO post) throws Exception {
//์ํฐํค์ ์คํ์ด์ค๋ฐ ์ฒ๋ฆฌ ์ ์ฝ ๋ก์ง์ ๊ฒ์๊ธ ๋ฑ๋ก์์ ์ฒ๋ฆฌ
String adjustCaption = post.getCaption()
.replace("\n", "<br>")
.replace("u0020", " ");
post.setCaption(adjustCaption);
postsDAO.insertPost(post);
}
์๋น์ค ๋จ์์ ๊ฒ์๊ธ ๋ฑ๋ก์ ์ํฐํค์ ์คํ์ด์ค๋ฐ๋ฅผ ์ฒ๋ฆฌํด์ฃผ์๋ค.
View
personal-write.jsp
<!-- ๊ฒ์๋ฌผ ์์ฑ ๊ณต๊ฐ -->
<article class="write-article">
<form class="write-form" action="<c:url value='/post/upload' />" method="post" enctype="multipart/form-data">
<input type="hidden" name="userNo" value="${login.userNo }">
<input type="hidden" name="id" value="${login.id }">
<div class="write-upload">
<!-- ์ด๋ฏธ์ง ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๋ถ๋ถ -->
<div class="left-side">
<img id="img" class="preview-img">
</div>
<!-- ๊ฒ์๊ธ ๋ด์ฉ ์์ฑ ๋ถ๋ถ -->
<div class="right-side">
<textarea class="input-textarea" name="caption" placeholder="๋ฌธ๊ตฌ ์
๋ ฅ..." style="resize: none;"></textarea>
</div>
</div>
<div class="write-upload">
<!-- ์ด๋ฏธ์ง ์
๋ก๋ ๋ฒํผ ๋ถ๋ถ -->
<div class="left-side fileBox" style="padding-right: 80px;">
<input type="file" id="input_img" name="file" style="width: 0; height: 0;">
</div>
<!-- ๊ณต์ ๋ฒํผ ๋ถ๋ถ -->
<div class="right-side">
<input type="submit" value="๊ณต์ " class="input-btn upload-btn" disabled="disabled">
</div>
</div>
</form>
<div class="left-side fileBox" style="padding-right: 80px;">
<button class="input-btn photo_find_btn">์ฐพ๊ธฐ</button>
</div>
</article>
๊ฒ์๋ฌผ ์์ฑ ๊ณต๊ฐ form
var sel_file;
$('#input_img').on("change", handleImgFileSelect);
function handleImgFileSelect(e) {
$('.upload-btn').attr('disabled', false);
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function(f){
sel_file = f;
var reader = new FileReader();
reader.onload = function(e) {
$("#img").attr("src", e.target.result);
}
reader.readAsDataURL(f);
});
}
$('.photo_find_btn').click(function(e) {
$("input:file").click();
});
ํ์ผ ์ด๋ฏธ์ง ์ฐพ๊ธฐ ๋ฒํผ
'Project > Instagram' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring] ํด๋ก ์ฝ๋ฉ16 Follow - Model, Repository, Mapper, Test, Service, Controller (0) | 2020.04.20 |
---|---|
[spring] ํด๋ก ์ฝ๋ฉ15 Post - ๊ฒ์๊ธ ์ญ์ (0) | 2020.04.20 |
[spring] ํด๋ก ์ฝ๋ฉ13 Post - ๊ฒ์๊ธ ์กฐํ (0) | 2020.04.20 |
[spring] ํด๋ก ์ฝ๋ฉ12 Post - Model, Repository, Mapper, Test, Service, Controller (0) | 2020.04.13 |
[spring] ํด๋ก ์ฝ๋ฉ11 User - ํ์ ํํด (0) | 2020.04.12 |
- Total
- Today
- Yesterday
- typeAliases
- ์ดํด๋ฆฝ์ค ์ค์น
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- ๋ณ๋ช ์ฒ๋ฆฌ
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- Algorithm
- ๊ฐ๋ฐ
- ์๋ฐ
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- ์จ๋ฆฌ์์ค
- ์๋ฃ๊ตฌ์กฐ
- ๊ฒ์๋ฌผ์กฐํ
- tomcat์ค์น
- ๊ฒ์ํ ์กฐํ
- ์๊ณ ๋ฆฌ์ฆ
- ๊ฒ์ํ ์ญ์
- java ํ๊ฒฝ๋ณ์
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- ๊ฒ์๋ฌผ ์ญ์
- mysql์ค์น
- ๋ถํธ ์๋์์ฑ
- java jdk ์ค์น
- 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 |