์ถํ ์ฐธ๊ณ ํ ์ฌ์ดํธ
์ด๋ฏธ์ง ์ ๋ก๋ (Spring+Ajax) (aejeong.com)
์ด๋ฏธ์ง ์ ๋ก๋ (Spring+Ajax)
Hyper ํ๋ก์ ํธ๋ฅผ ํ๋ฉด์ ๊ณผ์ ์ค ์ด๋ ค์ํ๋ ํ์ผ ์ ๋ก๋์ ๋ํด์ ๋ค์ ๊ณต๋ถํ๊ฒ ๋์๋ค. commons-fileupload๋ฅผ ์ฌ์ฉํ์๊ณ pom.xml์ ์ถ๊ฐํด์ค์ผ ํ๋ค. Ajax๋ก ๊ตฌํํด์ผ ํ๋ Controller์ @ResponseBody๋ฅผ ๋ถ.
aejeong.com
์๋ฃ์ค
PdsDto
package bit.com.a.dto;
import java.io.Serializable;
// PDS - Public Domain Software = ์๋ฃ์ค
public class PdsDto implements Serializable {
private int seq;
private String id;
private String title;
private String content;
private String filename; //์์ฉ์๊ฐ ์ฌ๋ฆฐ ๊ธฐ์กดํ์ผ์ด๋ฆ
private String newFilename; //DB์ ์ฅ์ ์ค๋ณต์ ํผํ๊ธฐ์ํด ์
๋ก๋๋ ์ง๋ก ๋ณ๊ฒฝํ ํ์ผ์ด๋ฆ
private int readcount; //์กฐํ์
private int downcount; //๋ค์ด๋ก๋์
private String regdate; //๊ฒ์๋ฌผ ์
๋ ฅํ ๋ ์ง
public PdsDto() {
}
public PdsDto(int seq, String id, String title, String content, String filename, String newFilename, int readcount,
int downcount, String regdate) {
super();
this.seq = seq;
this.id = id;
this.title = title;
this.content = content;
this.filename = filename;
this.newFilename = newFilename;
this.readcount = readcount;
this.downcount = downcount;
this.regdate = regdate;
}
public PdsDto(String id, String title, String content, String filename, String newFilename) {
super();
this.id = id;
this.title = title;
this.content = content;
this.filename = filename;
this.newFilename = newFilename;
}
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public String getId() {
return id;
}
```
Pdslist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table class="list_table" style="width: 85%">
<colgroup>
<col width="50"><col width="100"><col width="300"><col width="50">
<col width="50"><col width="50"><col width="100"><col width="50">
</colgroup>
<thead>
<tr>
<th>๋ฒํธ</th><th>์์ฑ์</th><th>์ ๋ชฉ</th><th>๋ค์ด๋ก๋</th>
<th>์กฐํ์</th><th>๋ค์ด์</th><th>์์ฑ์ผ</th><th>์ญ์ </th>
</tr>
</thead>
<tbody>
<c:forEach var="pds" items="${pdslist }" varStatus="i">
<tr>
<th>${i.count }</th>
<td>${pds.id }</td>
<td style="text-align: left;">
<a href="pdsdetail.do?seq=${pds.seq }">
${pds.title }
</a>
</td>
<td>
<input type="button" name="btnDown" value="๋ค์ด๋ก๋"
onclick="filedown('${pds.filename}','${pds.newFilename}', '${pds.seq }')">
</td>
<td>${pds.readcount }</td>
<td>${pds.downcount }</td>
<td>
<font size="1">${pds.regdate }</font>
</td>
<td>
<img alt="" src="image/del.png" data_file_seq="${pds.seq }" class="btn_fileDelete">
</td>
</tr>
</c:forEach>
</tbody>
</table>
<br><br>
<!--ํ์ด์ง -->
<div class="container">
<nav aria-label="Page navigation">
<ul class="pagination" id="pagination" style="justify-content:center;"></ul>
</nav>
</div>
<br><br>
<!-- ์๋ฃ์ถ๊ฐ ๋ฒํผ -->
<div id="button.wrap">
<span class="button blue">
<button type="button" id="_btnAdd">์๋ฃ์ถ๊ฐ</button>
</span>
</div>
<!-- ๋ค์ด๋ก๋ ๋ฒํผ์ ํด๋ฆญ์ -->
<form name="file_Down" action="fileDownload.do" method="post">
<input type="hidden" name="newFilename">
<input type="hidden" name="filename">
<input type="hidden" name="seq">
</form>
<script>
getBbsListCount();
$("#_btnAdd").click(function () {
location.href = "pdswrite.do";
});
function filedown(filename, newFilename, seq) {
alert("๋ค์ด๋ก๋ ํฉ๋๋ค");
let doc = document.file_Down;
doc.filename.value = filename;
doc.newFilename.value = newFilename;
doc.seq.value = seq;
doc.submit();
}
//paging ์ฒ๋ฆฌ
function loadPage( totalCount ) {
let pageSize = 10;
let nowPage = 1;
let _totalPages = totalCount / pageSize;
if(totalCount % pageSize > 0){
_totalPages++;
}
$("#pagination").twbsPagination('destroy'); // ํ์ด์ง ๊ฐฑ์ : ํ์ด์ง์ ๊ฐฑ์ ํด ์ค์ผ ๋ฒํธ๊ฐ ์ฌ์ค์ ๋๋ค.
$("#pagination").twbsPagination({
// startPage: 1,
totalPages: _totalPages,
visiblePages: 10,
first:'<span sria-hidden="true">«</span>',
prev:"์ด์ ",
next:"๋ค์",
last:'<span sria-hidden="true">»</span>',
initiateStartPageClick:false, // onPageClick ์๋ ์คํ๋์ง ์๋๋ก ํ๋ค
onPageClick:function(event, page){
nowPage = page;
// alert('nowPage:' + page);
getBbsListData( page - 1 );
}
});
}
//๊ธ์ ์ด์๋ฅผ ์ทจ๋
function getBbsListCount() {
$.ajax({
url:"./pdscount.do",
type:"get",
data:{ page:0 },
success:function( count ){
// alert('success');
// alert(count);
loadPage(count);
},
error:function(){
alert('error');
}
});
}
</script>
PdsController
@Controller
public class PdsController {
@Autowired
PdsService service;
@RequestMapping(value="pdslist.do", method = {RequestMethod.GET,RequestMethod.POST})
public String pdslist(Model model) {
model.addAttribute("doc_title", "์๋ฃ์ค๋ชฉ๋ก");
List<PdsDto> list = service.getPdsList();
model.addAttribute("pdslist", list);
return "pdslist.tiles";
}
์๋ฃ ์ ๋ก๋
-์ ๋ชฉ, ๋ด์ฉ ๋ฏธ์ ๋ ฅ์ ๊ธ์์ฑ ๋ถ๊ฐ๋ฅ(script)
-input type "file"๋ก ํ์ผ์ ๋ก๋ ๊ฐ๋ฅ
-enctype="multipart/form-data" ์ ์ก
multipart/form-data๋ ํ์ผ ์ ๋ก๋๊ฐ ์๋ ์์์์์ ์ฌ์ฉ๋๋ enctype ์์ฑ์ ๊ฐ์ค ํ๋์ด๊ณ , multipart๋ ํผ๋ฐ์ดํฐ๊ฐ ์ฌ๋ฌ ๋ถ๋ถ์ผ๋ก ๋๋์ด ์๋ฒ๋ก ์ ์ก๋๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<form name="frmFrom" id="_frmForm" action="pdsupload.do" method="post"
enctype="multipart/form-data">
<table class="list_table">
<tr>
<th>์์ด๋</th>
<td>
<input type="text" name="id" readonly="readonly" value="${login.id} " size="50">
</td>
</tr>
<tr>
<th>์ ๋ชฉ</th>
<td>
<input id="_title" type="text" name="title" size="50">
</td>
</tr>
<tr>
<th>ํ์ผ ์
๋ก๋</th>
<td>
<input type="file" name="fileload" size="50">
</td>
</tr>
<tr>
<th>๋ด์ฉ</th>
<td>
<textarea id="_content" rows="10" cols="50" name="content"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="height: 50px; text-align: center">
<a href="#none" id="_btnPds">
<img alt="" src="image/bwrite.png">
</a>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
$("#_btnPds").click(function () {
if( $("#_title").val().trim() == "" ){
alert("์ ๋ชฉ์ ์
๋ ฅํด ์ฃผ์ธ์");
$("#_title").focus();
}else if($("#_content").val().trim() == ""){
alert("๋ด์ฉ์ ์
๋ ฅํด ์ฃผ์ธ์");
$("#_content").focus();
}else{
$(_frmForm).submit();
}
});
</script>
๊ธ์ฐ๊ธฐ ๋ฒํผ ๋๋ฅผ์ controller์ด๋
MultipartFile์ ์ฌ์ฉํ File ์ ๋ก๋
(multipart/form-data)
SpringFrameworkํ๊ฒฝ์ ์๋ฒ๋ผ๋ฉด SpringFramework์์ ์ ๊ณตํ๊ณ ์๋ MultipartFile ํด๋์ค์ MultipartHttpServletRequest ํด๋์ค๋ฅผ ์ฌ์ฉํด์ File ์ ๋ก๋ ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์์ต๋๋ค
์ด ๋๊ฐ์ง ํด๋์ค๋ฅผ ์ฌ์ฉํด์ ์ด๋ป๊ฒ File ์ ๋ก๋ ๊ธฐ๋ฅ์ด ๊ตฌํ๋ ์ ์๋์ง Controller๋ก ๋ค์ด์ค๋ ์์ฒญ ํ๋ผ๋ฏธํฐ์ ํ์ ์ ๊ธฐ์ค์ผ๋ก ๋ณด๊ฒ ์ต๋๋ค.
์ฐธ๊ณ ๋ก ์ ๊ฐ ์ง ์ฝ๋๋ FIle ์ ๋ก๋ ๋ฐฉ์์ผ๋ก ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก HTTP ์์ฒญ์ ํ ๋, “Content-Type” ํ๋์ ๋ด์ฉ์ “multipart/form-data”๋ก ์ธํ ํด์ ์์ฒญํด์ผ ํฉ๋๋ค.
์๋์ ๊ฐ์ด ์ปจํธ๋กค๋ฌ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์คฌ์๋
DB์ ํ์ผ์ด๋ฆ์ด ์ค๋ณต๋์ด ์ ๋ก๋ ๋๋๊ฒ์ ๋ง์์ฃผ๊ธฐ์ํด ํ์ผ๋ช ์ ๋ณ๊ฒฝํด์ฃผ๋ ํจ์ ์ฌ์ฉ
package bit.com.a.utilex;
import java.util.Date;
public class PdsUtil {
//ํ์ผ๋ช
-> ๋ณ๊ฒฝ(time)
//myfile.txt - > f.indexOf('.') - >
//ํ์ผ๋ช
, ํ์ฅ์๋ช
//f.substring(6) -> .txt
public static String getNewFileName(String f) {
String filename = "";
String fpost = "";
if(f.indexOf('.')>0) {
//indexOf ์ง์ ๋ ์์๋ฅผ ์ฐพ๋๋ค
fpost = f.substring(f.indexOf('.'));
//.์ ์์น๋ถํฐ ๋๊น์ง ์๋ฅธ๋ค
System.out.println("fpost" + fpost);
filename = new Date().getTime() + fpost;
}else {
filename = new Date().getTime() + ".back";
}
return filename;
}
}
PdsController @RequetParam MultipartFile ํ์ ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
์์ฒญํ๋ผ๋ฏธํฐ ํ์ ์ @RequestParam ์ด๋ ธํ ์ด์ ๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์ ๋๋ค. ์ด ๊ฒฝ์ฐ, ์๋ฒ๋ ํด๋ผ์ด์ธํธ์ HTTP ์์ฒญ ๋ฉ์์ง์ ํฌํจ๋ ํ๋ผ๋ฏธํฐ๋ค์ ๋ชจ๋ ๋ถ๋ฆฌํด์ ์ปจํธ๋กคํ ์ ์๊ธฐ ๋๋ฌธ์, ์ปจํธ๋กค๋ฌ์ ์์ฒญํ๋ผ๋ฏธํฐ ํ์ ์ค MultipartFileํ์ ์ ํด๋นํ๋ ํ๊ฐ์ง๋ฅผ ์ง์ ๊ณจ๋ผ์, file ๋ฐ์ดํฐ๋ฅผ ์ง์ ๋ค๋ฃฐ ์ ์๋ค๋ ์ฅ์ ์ด ์์ง๋ง, file ๋ฐ์ดํฐ ์ธ์ ๋ค๋ฅธ ํ๋ผ๋ฏธํฐ๋ค(ex, String, Integer, MultipartFile)์ ๋ช๊ฐ์ง ์ถ๊ฐํด์ ํจ๊ป ๋ฐ๋๋ค๋ฉด ์ฝ๋๊ฐ ์กฐ๊ธ ์ง์ ๋ถํด์ง ์๋ ์๋ค๋ ๋จ์ ์ด ์์ต๋๋ค.
file.getOriginalFilename() : ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ก๋ํ ํ์ผ ์๋ณธ ์ด๋ฆ
req.getServletContext().getRealPath().getContextPath("/upload")
ํ์ผ ์ ๋ก๋ ์ ํ๋ก์ ํธ ๋ด๋ถ ํด๋๋ฅผ ์ด์ฉํ๊ธฐ ์ํด์ ์๋ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
@RequestMapping(value = "pdsupload.do", method = {RequestMethod.GET, RequestMethod.POST})
public String pdsupload(PdsDto pdsdto,
@RequestParam(value = "fileload", required = false)
MultipartFile fileload,
HttpServletRequest req) {
// filename ์ทจ๋
String filename = fileload.getOriginalFilename();
pdsdto.setFilename(filename); // ์๋ณธ ํ์ผ๋ช
์ ์ค์
// upload ๊ฒฝ๋ก ์ค์
// server(tomcat)
String fupload = req.getServletContext().getRealPath("/upload");
// ํด๋
// String fupload = "d:\\tmp";
System.out.println("fupload:" + fupload);
// ํ์ผ๋ช
๋ณ๊ฒฝ ์ฒ๋ฆฌ
String newfilename = PdsUtil.getNewFileName(pdsdto.getFilename());
pdsdto.setNewFilename(newfilename);
File file = new File(fupload + "/" + newfilename);
try {
// ์ค์ ๋ก ์
๋ก๋ ๋๋ ๋ถ๋ถ
FileUtils.writeByteArrayToFile(file, fileload.getBytes());
// db์ ์ ์ฅ
service.uploadPds(pdsdto);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:/pdslist.do";
}
'์คํ๋ง Spring > ๐งถ ์คํ๋ง ํ๋ ์์ํฌ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง Tiles๋ฅผ ํ์ฉํ ๊ฒ์ํ๋ง๋ค๊ธฐ (์บ๋ฆฐ๋ ์์ฑ, ์์ , ์ญ์ ) ํ์บ๋ฆฐ๋ ํ์ฉ (0) | 2021.06.25 |
---|---|
์คํ๋ง Tiles๋ฅผ ํ์ฉํ ๊ฒ์ํ (์์ฑ, ์์ , ์ญ์ , ๋ํ ์ผ ) (0) | 2021.06.24 |
Spring ํ์ผ ์ ๋ก๋ (0) | 2021.06.24 |
์คํ๋ง Tiles๋ฅผ ํ์ฉํ ๊ฒ์ํ ๋ง๋ค๊ธฐ (๊ฒ์ํ ํ์ด์ง, ๊ฒ์) (0) | 2021.06.22 |
Spring Model ๊ฐ์ฒด๋ ๋ฌด์์ผ๊น? (0) | 2021.06.22 |
๋๊ธ