답변하기 페이지 보여주기
1. 컨트롤러에서 매핑
@RequestMapping(value="boardReply.aws") // 답변하기 페이지 보여주기만
public String boardReply(@RequestParam("bidx") int bidx, Model model) { // 글번호 가지고 갈거고 모델에 담아서 보여주기
BoardVo bv = boardService.boardSelectOne(bidx); // 글 한개만 선택해서 가져오기
model.addAttribute("bv", bv); // bv모델에 담기
String path="WEB-INF/board/boardReply"; // 경로 답변하기로 보내주기
return path;
}
@RequestMapping(value="boardReplyAction.aws") // 답변 동작 구현
public String boardReplyAction(
BoardVo bv, // 제목, 내용, 작성자, 비밀번호 bv에 담아오기
@RequestParam("attachfile") MultipartFile attachfile, // 첨부파일은 멀티파트로
HttpServletRequest request, // 세션정보 담기 회원번호 목적
RedirectAttributes rttr) // 리다이렉트 객체 생성
throws Exception {
int value=0; // 결과값 초기화
MultipartFile file = attachfile;
String uploadedFileName="";
if (!file.getOriginalFilename().equals("")) { // 원본파일이 있으면
uploadedFileName = UploadFileUtiles.uploadFile(uploadPath, file.getOriginalFilename(), file.getBytes());
} // 여기에 담고
String midx = request.getSession().getAttribute("midx").toString();
int midx_int = Integer.parseInt(midx); // 회원번호 담기
String ip = getUserIp(request); // 아이피 정보 담기
bv.setUploadedFilename(uploadedFileName); // bv에 파일이름 세팅해주기
bv.setMidx(midx_int); //bv에 회원번호 세팅해주기
bv.setIp(ip); // bv에 아이피정보 세팅해주기
int maxBidx=0; //
maxBidx = boardService.boardReply(bv); //메서드 실행하고 나온 결과값 maxBidx에 담기
//System.out.println("maxBidx"+maxBidx);
String path="";
if (maxBidx !=0) {
path="redirect:/board/boardContents.aws?bidx="+maxBidx; // 결과값이 0이 아닌경우 > 성공한것이므로 글내용으로 보내기
}else {
rttr.addFlashAttribute("msg", "실패"); // 결과값이 0인 경우 > 실패 한 것이므로 다시 답변하기 페이지로 보내기
path="redirect:/board/boardReply.aws?bidx="+bv.getBidx();
}
return path; // 결과 경로 리턴해주기
}
public int boardReply(BoardVo bv);
3-2. serviceImpl에 선언
@Transactional // 두가지 이상 쿼리문 작동할 때 트랜잭션 걸기
@Override
public int boardReply(BoardVo bv) {
int value = bm.boardReplyUpdate(bv);
//System.out.println("value"+value);
int value2 = bm.boardReplyInsert(bv);
int maxBidx = bv.getBidx();
return maxBidx;
}
}
public int boardReplyUpdate(BoardVo bv);
public int boardReplyInsert(BoardVo bv);
4-2. boardMapper.xml에 쿼리 작성
<!-- 답변하기 기능 -->
<update id="boardReplyUpdate" parameterType="bv">
update board set depth= depth+1 where originbidx =#{originbidx} and depth > #{depth}
</update>
<!-- 답변한 결과물 삽입하기 -->
<insert id="boardReplyInsert" parameterType="bv">
<selectKey keyProperty="bidx" resultType="int" order="AFTER">
select max(bidx) as bidx from board
</selectKey>
insert into board (originbidx, depth, level_, subject, contents, writer, midx, filename, password, ip)
values(#{originbidx}, #{depth}+1, #{level_}+1, #{subject}, #{contents}, #{writer}, #{midx}, #{uploadedFilename}, #{password}, #{ip})
</insert>