-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
575 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package board.controller; | ||
|
||
import java.io.File; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import board.model.BoardDAOMyBatis; | ||
import board.model.BoardVO; | ||
import common.controller.AbstractAction; | ||
import user.model.UserVO; | ||
|
||
public class BoardDeleteAction extends AbstractAction { | ||
|
||
@Override | ||
public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
//로그인 체크 | ||
HttpSession session=req.getSession(); | ||
UserVO user=(UserVO)session.getAttribute("loginUser"); | ||
if(user==null) { | ||
req.setAttribute("msg", "로그인해야 글삭제가 가능해요"); | ||
req.setAttribute("loc", "javascript:history.back()"); | ||
|
||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
return; | ||
} | ||
|
||
// TODO Auto-generated method stub | ||
// [1] 삭제할 글 번호 받기 | ||
String numStr=req.getParameter("num"); | ||
|
||
// [2] 유효성 체크 => boardList.do redirect이동 | ||
if(numStr==null||numStr.trim().isEmpty()) { | ||
this.setViewPage("boardList.do"); | ||
this.setRedirect(true); | ||
return; | ||
} | ||
|
||
// [3] dao의 deleteBoard(num) | ||
BoardDAOMyBatis dao=new BoardDAOMyBatis(); | ||
|
||
//db에서 삭제할 해당 글 가져오기 | ||
BoardVO vo=dao.viewBoard(Integer.parseInt(numStr.trim())); | ||
|
||
if(!vo.getUserid().equals(user.getUserid())) { | ||
req.setAttribute("msg", "글쓴 사람만 삭제할 수 있어요"); | ||
req.setAttribute("loc", "javascript:history.back()"); | ||
|
||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
return; | ||
} | ||
|
||
|
||
if(vo.getFilename()!=null) { | ||
//첨부파일이 있다면 서버 Upload디렉토리에서 해당 파일을 먼저 삭제하자 | ||
String upDir=req.getServletContext().getRealPath("/Upload"); | ||
File delFile=new File(upDir, vo.getFilename()); | ||
if(delFile!=null) { | ||
delFile.delete(); | ||
} | ||
}//--------------------------------- | ||
|
||
int n=dao.deleteBoard(Integer.parseInt(numStr.trim())); | ||
|
||
// [4] 실행결과 메시지 및 이동 경로 지정 | ||
String str=(n>0)?"글 삭제 성공":"삭제 실패"; | ||
String loc="boardList.do"; | ||
|
||
req.setAttribute("msg", str); | ||
req.setAttribute("loc", loc); | ||
|
||
|
||
// [5] 뷰페이지 지정/이동방식 지정 | ||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package board.controller; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import com.oreilly.servlet.MultipartRequest; | ||
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy; | ||
|
||
import board.model.BoardDAOMyBatis; | ||
import board.model.BoardVO; | ||
import common.controller.AbstractAction; | ||
import user.model.UserVO; | ||
|
||
public class BoardEditAction extends AbstractAction { | ||
|
||
@Override | ||
public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
HttpSession session=req.getSession(); | ||
UserVO user=(UserVO)session.getAttribute("loginUser"); | ||
if(user==null) { | ||
req.setAttribute("msg", "로그인해야 글수정이 가능해요"); | ||
req.setAttribute("loc", "javascript:history.back()"); | ||
|
||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
return; | ||
} | ||
//0. post방식일때 한글 처리 | ||
//req.setCharacterEncoding("utf-8"); | ||
// 1. num, userid,subject,content, filename 값 받기 | ||
//1. 파일 업로드 처리==> 업로드 디렉토리의 절대경로 얻어오자. MvcWeb/src/main/webapp/Upload | ||
ServletContext application=req.getServletContext(); | ||
String upDir=application.getRealPath("/Upload"); | ||
System.out.println("upDir="+upDir); | ||
File dir=new File(upDir); | ||
if(!dir.exists()) { | ||
dir.mkdirs();//업로드 디렉토리 생성 | ||
} | ||
|
||
MultipartRequest mr=null; | ||
try { | ||
mr=new MultipartRequest(req, upDir,100*1024*1024,"UTF-8", new DefaultFileRenamePolicy()); | ||
System.out.println("파일 업로드 성공!!"); | ||
}catch(IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
String numStr=mr.getParameter("num"); | ||
String subject=mr.getParameter("subject"); | ||
String content=mr.getParameter("content"); | ||
String userid=user.getUserid(); | ||
String filename=mr.getFilesystemName("filename"); | ||
File file=mr.getFile("filename"); | ||
|
||
long filesize=0; | ||
if(file!=null) {//첨부한 파일이 있다면 | ||
filesize=file.length(); | ||
//예전에 첨한 파일명 얻기 | ||
String old_file=mr.getParameter("old_file"); | ||
if(old_file!=null&& !old_file.trim().isEmpty()) { | ||
//서버에서 예전 파일 지우기 | ||
File delFile=new File(upDir, old_file); | ||
if(delFile!=null) { | ||
boolean b=delFile.delete(); | ||
System.out.println("파일 삭제 여부: "+b); | ||
} | ||
} | ||
}//if--------- | ||
// 2. 유효성 체크 (num, subject,userid) | ||
if(numStr==null||subject==null||userid==null|| | ||
numStr.trim().isEmpty()||subject.trim().isEmpty()||userid.trim().isEmpty()) { | ||
this.setViewPage("boardList.do"); | ||
this.setRedirect(true); | ||
return; | ||
} | ||
int num=Integer.parseInt(numStr.trim()); | ||
|
||
//3. 1번에서 받은 값 BoardVO에 담아주기 | ||
BoardVO vo=new BoardVO(num,userid,subject, content,null,filename,filesize); | ||
|
||
//4. dao의 updateBoard(vo) | ||
BoardDAOMyBatis dao=new BoardDAOMyBatis(); | ||
int n=dao.updateBoard(vo); | ||
|
||
//5. req에 메시지, 이동경로 저장 | ||
String str=(n>0)?"글 수정 성공":"글 수정 실패"; | ||
String loc="boardList.do"; | ||
|
||
req.setAttribute("msg", str); | ||
req.setAttribute("loc", loc); | ||
|
||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package board.controller; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
import board.model.BoardDAOMyBatis; | ||
import board.model.BoardVO; | ||
import common.controller.AbstractAction; | ||
import user.model.UserVO; | ||
|
||
public class BoardEditFormAction extends AbstractAction { | ||
|
||
@Override | ||
public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
|
||
HttpSession session=req.getSession(); | ||
UserVO user=(UserVO)session.getAttribute("loginUser"); | ||
if(user==null) { | ||
req.setAttribute("msg", "로그인해야 글쓰기가 가능해요"); | ||
req.setAttribute("loc", "javascript:history.back()"); | ||
|
||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
return; | ||
} | ||
|
||
//1. 수정할 글 번호 받기 | ||
String numStr=req.getParameter("num"); | ||
//2. 유효성 체크 | ||
if(numStr==null||numStr.trim().isEmpty()) { | ||
this.setViewPage("boardList.do"); | ||
this.setRedirect(true); | ||
return; | ||
} | ||
BoardDAOMyBatis dao=new BoardDAOMyBatis(); | ||
//3. BoardVO dao.viewBoard(num) | ||
BoardVO vo=dao.viewBoard(Integer.parseInt(numStr.trim())); | ||
|
||
if(!vo.getUserid().equals(user.getUserid())) { | ||
req.setAttribute("msg", "글쓴 사람만 글 수정이 가능합니다"); | ||
req.setAttribute("loc", "javascript:history.back()"); | ||
|
||
this.setViewPage("message.jsp"); | ||
this.setRedirect(false); | ||
return; | ||
} | ||
|
||
//4. req에 저장 "board" | ||
req.setAttribute("board", vo); | ||
|
||
this.setViewPage("board/boardEdit.jsp"); | ||
this.setRedirect(false); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package board.controller; | ||
|
||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import board.model.BoardDAOMyBatis; | ||
import board.model.BoardVO; | ||
import common.controller.AbstractAction; | ||
|
||
public class BoardListAction extends AbstractAction { | ||
|
||
@Override | ||
public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
//0. 현재 보여줄 페이지(cpage)값 받아오기 | ||
String cpStr=req.getParameter("cpage"); | ||
if(cpStr==null||cpStr.trim().isEmpty()) { | ||
cpStr="1";//1페이지를 기본값으로 설정 | ||
} | ||
int cpage=Integer.parseInt(cpStr.trim()); | ||
if(cpage<1) { | ||
cpage=1;//1페이지를 기본값으로 설정 | ||
} | ||
|
||
//검색유형과 검색어 받기 | ||
String findType=req.getParameter("findType"); | ||
String findKeyword=req.getParameter("findKeyword"); | ||
|
||
if(findType==null) { | ||
findType=""; | ||
} | ||
if(findKeyword==null) { | ||
findKeyword=""; | ||
} | ||
|
||
|
||
BoardDAOMyBatis dao=new BoardDAOMyBatis(); | ||
//1. 총 게시글 수 구하기 | ||
int totalCount=dao.getTotalCount(findType, findKeyword); | ||
//2. 한 페이지 당 보여줄 목록 개수 정하기 | ||
int pageSize=5; | ||
|
||
//3. 페이지 수 구하기 | ||
/*totalCount pageSize pageCount | ||
1~4/ 5 5 1 | ||
6~9/ 10 5 2 | ||
11~14/ 15 5 3 | ||
if(totalCount%pageSize==0){ | ||
pageCount=totalCount/pageSize; | ||
}else{ | ||
pageCount=totalCount/pageSize +1; | ||
} | ||
pageCount=(totalCount-1)/pageSize +1; | ||
* */ | ||
int pageCount=(totalCount-1)/pageSize +1; | ||
|
||
if(pageCount<=0) { | ||
pageCount=1; | ||
} | ||
|
||
if(cpage>pageCount) { | ||
cpage=pageCount;//마지막 페이지로 지정 | ||
} | ||
|
||
int end=cpage * pageSize; | ||
int start = end -(pageSize-1); | ||
|
||
List<BoardVO> boardArr=dao.listBoard(start, end, findType, findKeyword); | ||
String qStr="&findType="+findType+"&findKeyword="+findKeyword; | ||
|
||
req.setAttribute("totalCount", totalCount); | ||
req.setAttribute("boardArr", boardArr); | ||
req.setAttribute("pageSize", pageSize); | ||
req.setAttribute("pageCount", pageCount); | ||
req.setAttribute("cpage", cpage); | ||
req.setAttribute("qStr", qStr); | ||
|
||
this.setViewPage("board/boardList.jsp"); | ||
this.setRedirect(false); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package board.controller; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import board.model.BoardDAOMyBatis; | ||
import board.model.BoardVO; | ||
import common.controller.AbstractAction; | ||
|
||
public class BoardViewAction extends AbstractAction { | ||
|
||
@Override | ||
public void execute(HttpServletRequest req, HttpServletResponse res) throws Exception { | ||
//1. 글 번호 받기 | ||
String numStr=req.getParameter("num"); | ||
//2. 유효성 체크 | ||
if(numStr==null||numStr.trim().isEmpty()) { | ||
this.setViewPage("boardList.do"); | ||
this.setRedirect(true); | ||
return; | ||
}//------------------------- | ||
int num=Integer.parseInt(numStr.trim()); | ||
//3. dao의 메서드 호출 | ||
BoardDAOMyBatis dao=new BoardDAOMyBatis(); | ||
|
||
BoardVO vo=dao.viewBoard(num); | ||
//4. req에 저장 | ||
req.setAttribute("board", vo); | ||
|
||
//5. 뷰페이지 지정, 이동방식 지정 | ||
this.setViewPage("board/boardView.jsp"); | ||
this.setRedirect(false); | ||
} | ||
|
||
} |
Oops, something went wrong.