list.jsp
1 2 3 4 5 6 7 8 9 10 | $(document).ready( function() { //현재 페이지 가져와서 해당 버튼의 리스트에 active 추가 let selectPage= ('${currentPage}' - 1) % 5; $(".pagenation>li.pagelist").removeClass("active"); $(".pagination>li.pagelist:eq(" + selectPage + ")").addClass( "active"); }); | cs |
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 76 77 78 79 80 81 82 83 84 85 86 | <div class="container pull-center"> <table class="table table-hover" width="50%"> <thead> <tr> <th>글번호</th> <th>제목</th> <th>글쓴이</th> <th>날짜</th> </tr> </thead> <tbody> <c:if test="${fn:length(tblBoardList)>0 }"> <c:forEach items="${tblBoardList }" var="row"> <tr> <td>${row.pkBoardIndex}</td> <td><c:choose> <c:when test="${row.colIssecret==true }"> <span><img src="../img/ic_lock_black_24dp_1x.png" style="width:1em;"></span> <a href="detail?index=${row.pkBoardIndex }">${row.colTitle }</a> </c:when> <c:otherwise> <a href="detail?index=${row.pkBoardIndex }">${row.colTitle }</a> </c:otherwise> </c:choose> <c:if test="${ row.colIsfile==true}"> <span><img src="../img/file.gif"></span> </c:if> <c:if test="${row.colReplycount>0 }"> <span> <img src="../img/iconReply.gif">[${ row.colReplycount}] </span> </c:if></td> <td>${row.colWriter }</td> <td>${row.colDate }</td> </tr> </c:forEach> </c:if> <c:if test="${fn:length(tblBoardList)==0 }"> <tr> <td colspan="4" class="text-center">게시글 없다</td> </tr> </c:if> </tbody> </table> <div class="text-center"> <ul class="pagination"> <c:if test="${currentPage>blockPage }"> <li class="page-item"><a class="page-link" href="list?currentPage=${startPage-1 }">이전</a></li> </c:if> <c:forEach var="i" begin="${startPage}" end="${endPage }" step="1"> <li class="page-item pagelist"><a class="btn page-link" href="/test/board/list?currentPage=${i}">${ i}</a></li> </c:forEach> <c:if test="${totalPage - startPage>blockPage }"> <li class="page-item"><a class="page-link" href="list?currentPage=${endPage+1 }">다음</a></li> </c:if> </ul> </div> <c:choose> <c:when test="${sessionScope.nick ==null }"> <jsp:include page="../member/login.jsp" /> </c:when> <c:otherwise> <div> ${sessionScope.nick }님 안녕하세요 <a class="btn btn-default pull-center" id="writeLink" href="writeForm">글쓰기</a> <a class="btn btn-default pull-center" id="writeLink" href="logout">로그아웃</a> </div> </c:otherwise> </c:choose> | cs |
detail.jsp
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 76 77 78 79 80 81 82 83 84 85 86 | $(document).ready(function() { $("#btnDelete").click(function() { if (confirm("이 글을 삭제하시겠습니까?")) { document.form1.action = "delete"; document.form1.submit(); } }); $("#btnUpdate").click(function() { document.form1.action = "writeForm" // 폼에 입력한 데이터를 서버로 전송 document.form1.submit(); }); }); </script> </head> <body> <form name="form1" method="post" class="text-center"> <div>작성일자 : ${tblBoard.colDate}</div> <div> 제목 <input name="colTitle" id="title" size="80" value="${tblBoard.colTitle}" readonly> </div> <div> 내용 <textarea name="colContent" id="content" rows="4" cols="80" readonly>${tblBoard.colContent}</textarea> </div> <div> 이름 <input name="colWriter" id="writer" value="${tblBoard.colWriter}" readonly> </div> <br> <br> <br> <ul class="list-group"> <c:if test="${fn:length(tblFiles) > 0 }"> 파일 내역 <c:forEach items="${tblFiles}" var="item"> <c:url var="download" value="download"> <c:param name="fileName">${item.colStoredName }</c:param> <c:param name="originalfileName">${item.colOriginalName }</c:param> </c:url> <li class="list-group-item"><a href="${download }">${item.colOriginalName }</a></li> </c:forEach> </c:if> </ul> <div> <br> <input type="hidden" name="pkBoardIndex" value="${tblBoard.pkBoardIndex}" /> <input type="hidden" name="colIssecret" value="${tblBoard.colIssecret}" /> <input type="hidden" name="colIsfile" value="${tblBoard.colIsfile }" /> <input type="hidden" name="colReplycount" value="${tblBoard.colReplycount }" /> <button type="button" id="btnUpdate" class="btn btn-primary">수정</button> <button type="button" id="btnDelete" class="btn btn-danger">삭제</button> </div> </form> <br> <ul class="list-group"> <c:forEach items="${ tblReplys}" var="item"> <li class="list-group-item row"> <div class="col-xs-8"> 작성자 : ${item.colWriter }</div> <div class="col-xs-4">내용 : ${item.colContent }</div> </li> </c:forEach> </ul> <Br> <form action="board/reply" class="text-center" method="post"> <div> <input type="hidden" name="index" value="${tblBoard.pkBoardIndex }" /> <input type="hidden" name="tblReply.fkBoardIndex" value="${tblBoard.pkBoardIndex }" /> <input type="hidden" name="tblReply.colWriter" value="${tblBoard.colWriter }" /> <textarea name="tblReply.colContent" rows="5" style="width: 30%;"></textarea> <button type="submit" id="btnReply" class="btn btn-default">댓글작성</button> </div> </form> </body> | cs |
write.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 | $(document).ready(function() { $("#btnSave").click(function() { var key = "${param.pkBoardIndex}"; if (key == "") { $("#form1").attr("action", "write"); $("#form1").submit(); } else { $("#form1").attr("action", "updatePost"); $("#form1").submit(); } }); var ckbox = $("#checkbox"); if ("${param.colIssecret}" == "true") { ckbox.attr("checked", "true"); } $("#addFilebtn") .click( function() { var div = $("#filerow div"); var len = div.length; var str = "<div><input type=text size=30 id=filetxt" + len + " readonly /><img src=../img/ic_open_in_browser_black_24dp_1x.png onclick=document.getElementById('fileinput" + len + "').click(); /> <img src=../img/ic_delete_black_24dp_1x.png onclick=$(this).parent('div').remove();> <input type=file size=30 id=fileinput" + len + " style='display: none;' name=upload onchange=document.getElementById('filetxt" + len + "').value=this.value; /></div>"; var clone = $("#filerow").append( str); }); }); | cs |
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 | <form id="form1" enctype="multipart/form-data" method="post" class="text-center"> <div> 제목 <input name="tblBoard.colTitle" id="title" size="80" value="${param.colTitle }"> </div> <div> 내용 <textarea name="tblBoard.colContent" id="content" rows="4" cols="80">${param.colContent }</textarea> </div> <div> 이름 <input type="text" name="tblBoard.colWriter" id="writer" readonly value="${sessionScope.nick }"> </div> <c:forEach items="${colOriginalName }" var="row"> ${row }<br> </c:forEach> <br> <br> <br> <input type="button" id="addFilebtn" value="파일추가" /> <div id="filerow"> <c:forEach items="${pkFileIndex }" var="row"> <input type="hidden" name="pkFileIndex" value="${row }" /> </c:forEach> <c:forEach items="${ colOriginalName }" var="row" varStatus="status"> <div> <input type="text" size="30" id="filetxt${status.index }" readonly name=oldInput value="${row}" /> <img src="../img/ic_open_in_browser_black_24dp_1x.png" onclick="document.getElementById('fileinput${status.index }').click();" /> <img src="../img/ic_delete_black_24dp_1x.png" onclick="$(this).parent('div').remove();"> <input name=oldFile type="file" size="30" id="fileinput${status.index }" style="display: none;" onchange="document.getElementById('filetxt${status.index }').value=this.value;" /> </div> </c:forEach> </div> <br> <br> <div> 비밀글<input id="checkbox" name="tblBoard.colIssecret" type="checkbox" value="true"> </div> <br> <div class="col-xs-6 col-xs-offset-3"> <button type="button" id="btnSave" class="btn btn-primary">등록</button> <button type="reset" class="btn btn-danger">리셋</button> </div> <input type="hidden" name="currentPage" value="${param.currentPage }"/> <input type="hidden" name="tblBoard.pkBoardIndex" value="${param.pkBoardIndex }" /> <input type="hidden" name="tblBoard.colReplycount" value="${param.colReplycount }" /> </form> | cs |
login
1 2 3 4 5 6 7 8 9 10 11 | <form action=login method=post> 아이디 : <input type="text" name="tblUser.pkId" size=12><br> 패스워드 : <input type="password" name="tblUser.colPassword" size=12><br> <input type=hidden> <input type=submit value='로그인'> </form> <form action=register method=post> <a type="submit" href="registerForm" onclick="w=window.open(this.href,'회원가입','toolbar=no, width=500,height=350'); w.focus(); return false;" >회원가입</a> </form> | cs |
PostAction
private TblBoard tblBoard;
private ArrayList<File> oldFile;
private ArrayList<String> oldFileContentType;
private ArrayList<String> oldFileFileName;
private ArrayList<File> upload;
private ArrayList<String> uploadContentType;
private ArrayList<String> uploadFileName;
private List<String> inputCount;
private List<String> oldInput;
private List<String> colStoredName;
private List<Integer> pkFileIndex;
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | public String updatePost() throws Exception { BoardDAO boardDAO = BoardDAO.getInstance(); FileDAO fileDAO = FileDAO.getInstance(); if (tblBoard.getColReplycount() == null) { tblBoard.setColReplycount(0); } if (oldInput != null) { int r = fileDAO.updateFlagByForeignKey(tblBoard.getPkBoardIndex()); System.out.println("모든 파일들 플래그 처리 결과 : " + r); for (int i = 0; i < oldInput.size(); i++) { if (oldInput.get(i).contains(":\\")) { int rnd = (int) (Math.random() * 100000); TblFile tblFile = new TblFile(pkFileIndex.get(i), tblBoard.getPkBoardIndex(), getOldFileFileName().get(0), getOldFileFileName().get(0) + rnd); File destFile = new File(fileUploadPath + getOldFileFileName().get(0) + rnd); FileUtils.copyFile(oldFile.get(0), destFile); oldFile.remove(0); oldFileFileName.remove(0); int rr = fileDAO.insertSelective(tblFile); } else { // 플래그를 true로 바꿔준다 int aa = fileDAO.updateFlagByPrimaryKey(pkFileIndex.get(i)); } } } // 새파일인 경우 if (upload != null) { int lastIndex = tblBoard.getPkBoardIndex(); ArrayList<TblFile> tblFiles = new ArrayList<>(); for (int i = 0; i < upload.size(); i++) { if (getUploadFileName() == null) { continue; } int rnd = (int) (Math.random() * 100000); tblFiles.add(new TblFile(lastIndex, getUploadFileName().get(i), getUploadFileName().get(i) + rnd)); File destFile = new File(fileUploadPath + getUploadFileName().get(i) + rnd); FileUtils.copyFile(getUpload().get(i), destFile); } fileDAO.insert(tblFiles); } if (upload != null || oldInput != null) { tblBoard.setColIsfile(true); } else { tblBoard.setColIsfile(false); } boardDAO.updateByPrimaryKey(tblBoard); return "success"; } public String insert() throws Exception { BoardDAO boardDAO = BoardDAO.getInstance(); FileDAO fileDAO = FileDAO.getInstance(); // 비밀글인지 체크, 파일수 체크 if (upload != null) { System.out.println("파일 있네요"); tblBoard.setColIsfile(true); } else { System.out.println("파일 없네요"); tblBoard.setColIsfile(false); } if (tblBoard.getColIssecret() == null) { System.out.println("비밀글 체크안했어"); tblBoard.setColIssecret(false); } else { System.out.println("비밀글 체크했어"); tblBoard.setColIssecret(true); } boardDAO.insert(tblBoard);// insert 후에 그 번호를 가져올수있도록. // 파일 넣기 if (upload != null) { int lastIndex = boardDAO.selectLastIndex(); ArrayList<TblFile> tblFiles = new ArrayList<>(); for (int i = 0; i < upload.size(); i++) { if (getUploadFileName() == null) { continue; } int rnd = (int) (Math.random() * 100000); tblFiles.add(new TblFile(lastIndex, getUploadFileName().get(i), getUploadFileName().get(i) + rnd)); File destFile = new File(fileUploadPath + getUploadFileName().get(i) + rnd); FileUtils.copyFile(getUpload().get(i), destFile); } System.out.println("업로드할 파일 객체들 " + tblFiles); int result = fileDAO.insert(tblFiles); } return "success"; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public String getEncSHA256(String txt) throws Exception { StringBuffer sbuf = new StringBuffer(); MessageDigest mDigest = MessageDigest.getInstance("SHA-256"); mDigest.update(txt.getBytes()); byte[] msgStr = mDigest.digest(); for (int i = 0; i < msgStr.length; i++) { byte tmpStrByte = msgStr[i]; String tmpEncTxt = Integer.toString((tmpStrByte & 0xff) + 0x100, 16).substring(1); sbuf.append(tmpEncTxt); } return sbuf.toString(); } |
ListAction
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 | private Integer index; private List<TblBoard> tblBoardList; private int currentPage = 1; private int totalPage; private int blockCount = 10; private int blockPage = 5; private int startPage; private int endPage; @Override public String execute() throws Exception { BoardDAO boardDAO = BoardDAO.getInstance(); // DB에 한번만접근하기 (규모가 작으니까 한꺼번에 한번 가져오는 방식) tblBoardList = boardDAO.selectAll(); int rowCount = tblBoardList.size(); totalPage = ((rowCount - 1) / blockCount) + 1; if (totalPage < currentPage) currentPage = totalPage; if (currentPage <1) currentPage = 1; int startCount = (currentPage - 1) * blockCount; int endCount = startCount + blockCount; startPage = (int) ((currentPage - 1) / blockPage) * blockPage + 1; endPage = startPage + blockPage - 1; if (endPage > totalPage) endPage = totalPage; if (endCount > rowCount) endCount = rowCount; tblBoardList = tblBoardList.subList(startCount, endCount); return SUCCESS; } | cs |
DetailAction
private Integer index;
private TblBoard tblBoard;
private List<TblFile> tblFiles;
private List<TblReply> tblReplys;
tblBoard = boardDAO.selectByPrimaryKey(index);
tblFiles = fileDAO.selectByForeignKey(index);
tblReplys = replyDAO.selectByForeignKey(index);
FileDownloadAction
private String inputPath = "D:\\upload\\";
private String fileName;
private String originalfileName;
private String contentType;
private String contentDisposition;
private InputStream inputStream;
private long contentLength;
@Override
public String execute() throws Exception {
File f = new File(inputPath + fileName);
setContentLength(f.length());
setContentDisposition("attachment; filename=" + URLEncoder.encode(originalfileName, "UTF-8"));
setInputStream(new FileInputStream(inputPath + fileName));
return SUCCESS;
}
BoardDAO
: selectAll() = 전체 리스트 가져오기
: insert(TblBoard) = 객체 넣기
: updateByPrimaryKey = 객체 업데이트
: selectLastIndex = 마지막 인덱스 가져오기 ?? 이거 필요할까?
: selectByPrimaryKey = 선택키 객체 가져오기
: deleteByPrimaryKey = 선택키 삭제
: increaseReplyCount = 키로 리플카운트 증가
: decreaseReplyCount = 키로 리플카운트 감소
FileDAO
: insert = 파일 객체 넣기
: selectByForeignKey 키로 해당 파일리스트 가져오기
: deleteByForeignKey 키로 해당 파일들 삭제하기
: updateFlagByForeignKey(int fkBoardIndex)
: int updateFlagByPrimaryKey(int pkFileIndex)
: int insertSelective(TblFile tblFile)
ReplyDAO
: insert = 리플 객체 넣기
: selectByForeignKey 해당 키로 리플들 가져오기
UserDAO
: selectUserConfirm 유저 객체 넣어서 해당 유저 객체 가져오기
: insert = 유저 객체 넣기
DAO 샘플
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 | public static class ReplyDAOHolder { private static final ReplyDAO instance = new ReplyDAO(); } public static ReplyDAO getInstance() { return ReplyDAOHolder.instance; } private SqlSessionFactory sqlSessionFactory; public ReplyDAO() { sqlSessionFactory = SqlSessionFactoryService.getSqlSessionFactory(); } public int insert(TblReply tblReply) { SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.openSession(true); TblReplyMapper tblReplyMapper = sqlSession.getMapper(TblReplyMapper.class); return tblReplyMapper.insert(tblReply); } catch (Exception e) { return 0; } finally { if (sqlSession != null) sqlSession.close(); } } | cs |
'웹' 카테고리의 다른 글
PHP 페이징 + Ajax (코드이그나이터) (1) | 2018.07.11 |
---|---|
jquery 선택자 정리 (0) | 2018.07.09 |
HTML CSS 활용 (생활코딩) (1) | 2018.04.25 |
페이징 속도 빠르게 하는 하는 DB 쿼리 (0) | 2018.04.25 |
struts 2.0 + mybatis3 INSERT 과정 (0) | 2018.03.29 |