ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

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";
}

personal-write.jsp

์ธ์Šคํƒ€๊ทธ๋žจ ์›นํŽ˜์ด์ง€์—์„œ๋Š” ๊ฒŒ์‹œ๊ธ€์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์—†๋„๋ก ๋˜์–ด์žˆ๋‹ค. ํ•˜์Šคํƒ€๊ทธ๋žจ์—์„œ๋Š” ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐœ์ธ์ ์œผ๋กœ ํ‹€์„ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค. ํ”„๋ก ํŠธ์—”๋“œ ๋‹ค๋ฃจ๋Š”๊ฒŒ ์ต์ˆ™์น˜ ์•Š์•„์„œ ๋ฐ‘์— ๋ฒ„ํŠผ ๋‘๊ฐœ๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š”๋ฐ ์–ด๋ ค์›€์„ ๋Š๊ผˆ๋‹ค. ๊ฒฐ๊ตญ ๋ฐฐ์น˜๋Š” ์œ„์— ์‚ฌ์ง„์ƒํƒœ๋กœ ์™„์„ฑ ๋˜์—ˆ์ง€๋งŒ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ๋“ค ๋งŒ๋“ค๊ณ  ๋‹ค์‹œ ๊ฑด๋“ค์—ฌ๋ด์•ผ๊ฒ ๋‹ค.

 

 

//๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ์š”์ฒญ
@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", "&nbsp;");
	
	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();
});

 ํŒŒ์ผ ์ด๋ฏธ์ง€ ์ฐพ๊ธฐ ๋ฒ„ํŠผ

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€