3.(스프링4 오라클자바커뮤니티 게시판)[컨트롤러, AOP,미리보기서블릿]Spring JDBC]Spring4@MVC-멀티파일업로드,답변형,JSR303 @Valid,@Controller,@ModelAttribute,@SessionAttributes,어노테이션기반
1. 리스트보기에서 본문 미리보기용 서블릿(ajax)
package onj.board.ajaxpreview;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import onj.board.service.BoardService;
import onj.board.service.BoardServiceHelper;
//Servlet3.0이상, Tomcat7 이상에서는 애노테이션으로 서블릿 정의가 가능하다.
@WebServlet(urlPatterns = "/preView")
public class ContentPreview extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String seq = req.getParameter("seq");
req.setCharacterEncoding("utf-8");
res.setContentType("text/html; charset=utf-8");
res.setHeader("Cache-Control", "no-cache");
BoardService boardService = BoardServiceHelper.getBoardService(getServletContext());
PrintWriter out = res.getWriter();
out.println("<pre>" + boardService.preView(seq) + "<pre>");
}
}
2. Spring AOP용 Aspect클래스
package onj.board.aop;
import onj.board.dao.BoardDAO;
import onj.board.dao.LoggingDAO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
@Aspect
public class LoggingAspect {
@Autowired
private LoggingDAO loggingDAO;
@Autowired
private BoardDAO boardDAO;
// 사후충고(반드시 정상 리턴 후 호출)
// BoardDAOImpl의 모든메소드중 파라미터가 1개 있는 메소드가 충고받을 포인트컷
// 본 게시판에는 리스트보기와 , getSql() 만 메소드 파라미터가 없어 충고 적용 안됨
@AfterReturning(pointcut="execution(* onj.board.dao.BoardDAOImpl.*(*))", returning="result")
public void logAfterReturning(JoinPoint joinPoint) {
System.out.println("<<<<<<<<< DAO 로깅 충고 실행");
loggingDAO.writeLog(joinPoint.getSignature().getName(), boardDAO.getSql());
}
}
3. 컨트롤러
package onj.board.controller;
import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import onj.board.model.BoardDTO;
import onj.board.model.CommentDTO;
import onj.board.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
@SessionAttributes("read") //게시물읽기에서 읽은 게시물을 수정을 위해 세션에 보관
public class BoardMultiController {
@Autowired
private BoardService boardService;
// 게시판 리스트 보기, 페이징 기능은 구현 안함
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView mv = new ModelAndView("list", "list",
boardService.boardList());
return mv;
}
// 게시글 읽기
@RequestMapping("/read")
public String read(@RequestParam("seq") String seq, Model model) {
// 게시물 한건 내용, "read"라는 이름으로 세션에 저장된다.(@SessionAttributes)
model.addAttribute("read", boardService.readContent(seq));
// comments(ArrayList에 CommentDTO가 담겨져 있다.)
model.addAttribute("comments", boardService.commentList(seq));
return "read";
}
// 커멘트쓰기
@RequestMapping("/comment")
public ModelAndView comment(@ModelAttribute CommentDTO commentDTO,
@ModelAttribute BoardDTO read) {
boardService.insertComment(commentDTO);
return new ModelAndView("redirect:/read.html?seq=" + read.getSeq());
}
// 게시판 글쓰기 화면
@RequestMapping(value = "/write", method = RequestMethod.GET)
public String write_view(Model model) {
if (!model.containsAttribute("uploadForm")) {
model.addAttribute("uploadForm", new BoardDTO());
}
return "write";
}
@RequestMapping(value = "/writeok", method = RequestMethod.POST)
public ModelAndView writeok(
@ModelAttribute("uploadForm") @Valid BoardDTO uploadForm ,
BindingResult result ) throws Exception {
if (result.hasErrors()) return new ModelAndView("write");
// 먼저 파일 업로드, 폴더는 미리 만드세요
// 스프링의 MultipartFile을 이용한 업로드
List<MultipartFile> files = uploadForm.getFiles();
if (null != files && files.size() > 0) {
int i = 0;
for (MultipartFile multipartFile : files) {
i++;
String fileName = multipartFile.getOriginalFilename();
if (!"".equals(fileName)) {
String path = "c:/java/project/spring4board/upload/"
+ fileName;
File f = new File(path);
multipartFile.transferTo(f);
if (i == 1) {
uploadForm.setFileName1(fileName);
} else if (i == 2) {
uploadForm.setFileName2(fileName);
} else if (i == 3) {
uploadForm.setFileName3(fileName);
}
}
}
}
boardService.insertBoard(uploadForm);
return new ModelAndView("redirect:/list.html");
}
// 게시글 수정, 세션에 있는 read 객체(BoardDTO, 게시물한건)가
// 사용자 파라미터 값을 받아 인자로 삽입된다.
@RequestMapping("/update")
public ModelAndView update(@ModelAttribute BoardDTO read) {
boardService.updateBoard(read);
return new ModelAndView("redirect:/read.html?seq=" + read.getSeq());
}
// 게시글 삭제
@RequestMapping("/delete")
public ModelAndView delete(@RequestParam("seq") String seq,
@RequestParam("passwd") String passwd, HttpServletResponse res)
throws Exception {
int result = boardService.deleteBoard(seq, passwd);
if (result != 1) {
PrintWriter out = res.getWriter();
out.println("<script>alert('password not correct');</script>");
out.println("<script>history.go(-1);</script>");
return null;
} else {
return new ModelAndView("redirect:/list.html");
}
}
// 게시물상세보기에서 답변 클릭시 호출되어 답변달 reply.jsp로 연결
@RequestMapping("/reply")
public String reply(Model model) {
if (!model.containsAttribute("replyForm")) {
model.addAttribute("replyForm", new BoardDTO());
}
return "reply";
}
// 답글 저장
@RequestMapping("/replyok")
public ModelAndView replyok(@ModelAttribute("replyForm") @Valid BoardDTO replyForm,
BindingResult result) {
if (result.hasErrors()) return new ModelAndView("reply");
boardService.replyBoard(replyForm);
return new ModelAndView("redirect:/list.html");
}
}
평일주간[100%환급과정]
(8/25)C#4.0,WinForm,ADO.NET(8/25)안드로이드개발자과정
(8/25)SQL기초에서 Schema Object까지
(8/29)Spring,MyBatis,Hibernate실무과정
(8/29)자바기초JDBC,Servlet/JSP까지
(8/29)PL/SQL,ORACLE HINT,TUNING
(8/25)오라클자바채용확정교육
평일야간[개인80%환급]
(8/21)Spring, MyBatis, Hibernate
(8/21)HTML5,CSS3,Ajax,jQuery마스터
(8/21)C#,Network,ADO.NET,ASP.NET
(8/26)SQL기초에서실무까지
(8/26)안드로이드개발자과정
(8/28)자바JSP,jQuery,Spring,MyBatis
주말주간[개인80%환급]
(8/23)자바웹&스프링,마이바티스
(8/23)Spring, MyBatis, Hibernate
(8/23)SQL기초에서실무까지
(8/23)자바,네트워크,웹&스프링
(8/30)안드로이드개발자과정
(8/30)C#,ASP.NET마스터(8/30)웹퍼블리싱 마스터
주말야간[개인80%환급]
(8/23)SQL기초에서실무까지
(8/23)자바,네트워크,웹&스프링
댓글 없음:
댓글 쓰기