[Spring] 게시판, Modal창 뜨게하기

yedy·2023년 6월 27일
0

[Spring]

목록 보기
7/7
post-thumbnail

1.게시판에서 글쓰기 버튼을 누르면 글쓰기 폼으로 이동한다.
2. 글 등록이 완료되면 list페이지로 넘어간다.
3. 넘어간 페이지에서 글 번호(bno)와 함께 모달창이 뜨게 하고싶다.

list.jsp에 우선 modal 폼을 추가해준다.

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
           <div class="modal-content">
                 <div class="modal-header">
                       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                           <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                       </div>
                       <div class="modal-body">
                  			글 등록이 완료되었습니다.
                       </div>
                <div class="modal-footer">
         			<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
          <!-- /.modal-content -->
           </div>
  <!-- /.modal-dialog -->
    </div>
       <!-- /.modal -->

ajax 부분이다..

$("#regbtn").on("click", function(){
		// window.location : "/board/register";  -> 전역 객체 ..옛날 버젼입니당 걍 밑에꺼 써
		// self.location = "/board/register";   -> 현재 페이지의 URL을 변경하면 해당 URL로 리디렉션 되는 기능
		self.location = "/board/register";
	});
	
	var result = "${result}";
	
	checkModal(result);
	
	// history : 자바 스크립트가 가진 객체 -> 브라우저의 세션 기록을 변경하는 역할
	// {} :상태(데이터 포함가능), title, url
	history.replaceState({},null,null); //브라우저가 새롭게 호출되서 기록에 남지 않도록 설정
	
	function checkModal(result) {
		if(result === '' || history.state){
			return;
		}
		if(parseInt(result) > 0){
			$(".modal-body").html("게시글 "+parseInt(result)+"번 등록 완료.");
		}$("#myModal").modal("show");
	}

controller 부분

	@PostMapping("/register")
	// RedirectAttributes : 객체를 매개변수로 받는 메소드 -> 리다이렉트시 데이터를 전달하기 위해 사용
	public String register(BoardVO board,RedirectAttributes rttr) {
		Integer bno = service.registerKey(board);
		//log.info("bno : " +bno);
		
		// name과 value binding (연결,바인딩!!)
		// rttr.addAttribute("result",bno);  // url뒤에 파라미터가 따라붙는 방식
		
		// 한번만 전송 가능해..
		rttr.addFlashAttribute("result", bno);
		return "redirect:/board/list";
	}

mapper부분

	<insert id="insertKey" parameterType="org.spring.domain.BoardVO"
		keyProperty="bno" keyColumn="bno" useGeneratedKeys="true">
		INSERT INTO
		tbl_board (title, content, writer)
		VALUES (#{title}, #{content},
		#{writer})
	</insert>


bno가 잘 전달되어 모달창이 뜰 수 있는 것을 볼 수 있다!

profile
공주 개발자

0개의 댓글