writeForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>게시판</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<center><b>글쓰기</b>
<br>
<form method="post" action="writePro.do" enctype="multipart/form-data">
<table width="400" border="1" cellspacing="0" cellpadding="0" align="center">
<c:set var="num" value="${requestScope.num }" />
<c:set var="ref" value="${requestScope.ref}" />
<c:set var="re_step" value="${requestScope.re_step}" />
<c:set var="re_level" value="${requestScope.re_level}" />
<input type="hidden" name="num" value="${num}" />
<input type="hidden" name="ref" value="${ref}" />
<input type="hidden" name="re_step" value="${re_step}" />
<input type="hidden" name="re_level" value="${re_level}" />
<tr>
<td width="70" align="center">이 름</td>
<td align="left" width="330">
<input type="text" size="10" maxlength="10" name="writer" >
</tr>
<tr>
<td width="70" align="center" >제 목</td>
<td align="left" width="330">
<input type="text" size="40" maxlength="50" name="subject" ></td>
</tr>
<tr>
<td width="70" align="center">Email</td>
<td align="left" width="330">
<input type="text" size="40" maxlength="30" name="email" ></td>
</tr>
<tr>
<td width="70" align="center">파일 첨부</td>
<td align="left" width="330">
<input type="file" id="reportFile" name="reportFile">
</td>
</tr>
<tr>
<td width="70" align="center" >내 용</td>
<td align="left" width="330">
<textarea name="content" rows="13" cols="40"></textarea></td>
</tr>
<tr>
<td width="70" align="center" >비밀번호</td>
<td align="left" width="330" >
<input type="password" size="8" maxlength="12" name="passwd">
</td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" name="save" value="글쓰기" >
<input type="reset" value="다시작성">
<input type="button" value="목록보기"
onclick="document.location.href='boardList.do?pageNum=${pageNum}'">
</td>
</tr>
</table>
</form>
</body>
</html>
글쓰기 , 수정, 삭제의 경우는 springBoardSimpleController를 이용합니다.
이것은 스프링의 SimpleFormController를 상속한 것이며, SimpleFormController에 대한 자세한 내용은 책이나 다른자료를 참고하시기 바랍니다.
package web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import board.Board;
import board.Comment;
import board.FileData;
import board.FileUploadBean;
import boardDao.BoardCommentDao;
import boardDao.BoardDeleteDao;
import boardDao.BoardUpdateDao;
import boardDao.BoardWriteDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringrewriteFormController extends SimpleFormController {
protected final Log logger = LogFactory.getLog(getClass());
private String pageName="";
private BoardWriteDao boardWriteDao;
private BoardDeleteDao boardDeleteDao;
private BoardUpdateDao boardUpdateDao;
private BoardCommentDao boardCommentDao;
//private MultipartFile reportFile;
//private byte[] sourceCode;
private FileUploadBean uploadFile;
public BoardCommentDao getBoardCommentDao() {
return boardCommentDao;
}
public void setBoardCommentDao(BoardCommentDao boardCommentDao) {
this.boardCommentDao = boardCommentDao;
}
public BoardWriteDao getBoardWriteDao() {
return boardWriteDao;
}
public void setBoardWriteDao(BoardWriteDao boardWriteDao) {
this.boardWriteDao = boardWriteDao;
}
public BoardDeleteDao getBoardDeleteDao() {
return boardDeleteDao;
}
public void setBoardDeleteDao(BoardDeleteDao boardDeleteDao) {
this.boardDeleteDao = boardDeleteDao;
}
public BoardUpdateDao getBoardUpdateDao() {
return boardUpdateDao;
}
public void setBoardUpdateDao(BoardUpdateDao boardUpdateDao) {
this.boardUpdateDao = boardUpdateDao;
}
private void fileUpload(HttpServletRequest request) throws IOException, SQLException {
FileData data = new FileData();
//InputStream fn = reportFile.getInputStream();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
MultipartFile file = multipartRequest.getFile("reportFile");
String path="c:\\upload";
String filename= file.getName();
String orignalFileName=file.getOriginalFilename();
String contentType = file.getContentType();
logger.info("insert --"+contentType);
long fileSize= file.getSize();
UUID randUUID =UUID.randomUUID();
String oranizeFilepath=path+"/"+ randUUID+"_"+orignalFileName;
data.setNum(boardWriteDao.getMaxNum());
data.setFilename(randUUID+"_"+orignalFileName);
data.setOriginalname(orignalFileName);
data.setFilesize(fileSize);
data.setContentType(contentType);
boardWriteDao.insertFileData(data);
//파일 용량 체크 어떻게??
//파일 확장자 체크
InputStream inputStream = null;
OutputStream outputStream = null;
try{
if(fileSize>0){
inputStream = file.getInputStream();
//File realUploadDir = new File()
//CommonsMultipartResolver com = new CommonsMultipartResolver();
//file.
File realUploadDir = new File(path);
if(!realUploadDir.exists()){
realUploadDir.mkdirs();
}
outputStream = new FileOutputStream(oranizeFilepath);
//버퍼를 지정해서 올린다.
int readBytes = 0;
byte [] buffer = new byte[8192];
while((readBytes=inputStream.read(buffer,0,8192))!=-1){
outputStream.write(buffer,0,readBytes);
}
}
}catch(Exception ex){
} finally{
outputStream.close();
inputStream.close();
}
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
logger.info("onSubmit");
Board board = (Board)command;
logger.info("num "+board.getNum());
if(request.getParameter("save")!=null) {
logger.info("save_insert");
insertLogic(request,command);
//fileUpload(request,);
return new ModelAndView("writePro");
}else if(request.getParameter("delete")!=null){
logger.info("delete_remove");
deleteLogic(request, command);
}else if(request.getParameter("update")!=null) {
logger.info("update_modify");
updateLogic(request,command);
return new ModelAndView("updatePro");
}else if(request.getParameter("comment")!=null){
logger.info("insert_comment");
insertCommentLogic(request, command);
}
return new ModelAndView(this.getSuccessView());
}
private void insertCommentLogic(HttpServletRequest request, Object command )throws SQLException {
Comment data = new Comment();
int num = Integer.parseInt(request.getParameter("num"));
String name = request.getParameter("name");
String comment_content = request.getParameter("comment_content");
logger.info("num"+num+"name"+name+"comment"+comment_content);
data.setNum(num);
data.setName(name);
data.setComment_content(comment_content);
boardCommentDao.insertComment(data);
}
private void deleteLogic(HttpServletRequest request,Object command)throws SQLException {
String pageNum=request.getParameter("pageNum");
int num=Integer.parseInt(request.getParameter("num"));
String passwd=request.getParameter("passwd");
int check=boardDeleteDao.deleteBoardData(num, passwd);
logger.info("check "+check);
logger.info("check "+pageNum);
request.setAttribute("pageNum",new Integer(pageNum));
request.setAttribute("check", new Integer(check));
}
private void updateLogic(HttpServletRequest request,Object command)throws SQLException {
Board board =(Board)command;
int num = board.getNum();//Integer.parseInt(request.getParameter("num"));
String pageNum =request.getParameter("pageNum1");
logger.info(pageNum);
int check=boardUpdateDao.updateBoard(num,board);
request.setAttribute("pageNum",new Integer(pageNum));
request.setAttribute("check", new Integer(check));
}
private void insertLogic(HttpServletRequest request,Object command) throws SQLException, IOException{
Board board=(Board)command;
int number=0;
int num=Integer.parseInt(request.getParameter("num"));
number=boardWriteDao.getMaxNum();
int ref=Integer.parseInt(request.getParameter("ref"));
int re_step=Integer.parseInt(request.getParameter("re_step"));
int re_level=Integer.parseInt(request.getParameter("re_level"));
board.setReg_date(new Timestamp(System.currentTimeMillis()));
board.setIp(request.getRemoteAddr());
logger.info("maxNum "+number);
logger.info("num "+num+"ref"+ref+"re_step"+re_step+"re_level"+re_level+board.getWriter());
if(number!=0){
number=number+1;
}else{
number=1; //새글의 번호는 1이다.
}
if(num!=0){
logger.info("number is"+num);
HashMap map = new HashMap();
map.put("ref", ref);
map.put("re_step", re_step);
boardWriteDao.updateBoardData(map);
re_step=re_step+1;
re_level=re_level+1;
logger.info("re_step"+re_step+"re_level"+re_level);
}else{
//ref=number;
board.setRef(number);
re_step=0;
re_level=0;
}
board.setRe_step(re_step);
board.setRe_level(re_level);
boardWriteDao.insertBoardData(board);
//data.setNum(num);
fileUpload(request);
}
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
Board board = new Board();
String [] subject = request.getRequestURI().split("/");
logger.info("uri "+subject[1]);
logger.info("uri "+subject[2]);
if(subject[2].equals("updateForm.do")){
request.setCharacterEncoding("euc-kr");
pageName="updateForm";
logger.info(pageName);
this.setFormView(pageName);
request.setAttribute("num", request.getParameter("num"));
request.setAttribute("pageNum", request.getParameter("pageNum"));
board.setNum(Integer.parseInt(request.getParameter("num")));
board.setWriter(request.getParameter("writer"));
board.setEmail(request.getParameter("email"));
board.setReadcount(Integer.parseInt(request.getParameter("readcount")));
board.setReg_date(Timestamp.valueOf(request.getParameter("reg_date")));
board.setSubject(request.getParameter("subject"));
board.setContent(request.getParameter("content"));
request.setAttribute("board", board);
}
else if(subject[2].equals("deleteForm.do")){
pageName="deleteForm";
this.setFormView(pageName);
}
else if(subject[2].equals("writeForm.do")){
//pageName="writeForm";
logger.info("writeForm.do===");
this.setFormView("writeForm");
}
if(request.getParameter("command")!=null){
logger.info("command");
if(request.getParameter("command").equals("insert")){
int num=0,ref=1,re_step=0,re_level=0;
try{
if(request.getParameter("num")!=null){
num=Integer.parseInt(request.getParameter("num"));
ref=Integer.parseInt(request.getParameter("ref"));
re_step=Integer.parseInt(request.getParameter("re_step"));
re_level=Integer.parseInt(request.getParameter("re_level"));
}
}catch(Exception e){e.printStackTrace();}
//해당 뷰에서 사용할 속성
request.setAttribute("num",num);
request.setAttribute("ref",ref);
request.setAttribute("re_step",re_step);
request.setAttribute("re_level",re_level);
}
//글쓰기 폼으로 갈려고한다면..
}
return board;
}
public FileUploadBean getUploadFile() {
return uploadFile;
}
public void setUploadFile(FileUploadBean uploadFile) {
this.uploadFile = uploadFile;
}
}
댓글 없음:
댓글 쓰기