스프링 프레임워크

form 속성, redirect:

tttck88 2019. 5. 4. 19:58

<form>의 method 속성이 'post'로 지정된 것과 action아 지정되지 않은 것을 볼 수 있다.

이런 경우에는 현재경로를 그대로 action의 대상 경로로 잡게 된다.

<form role="form" method="post">
	<div class="box-body">
		<div class="form-group">
			<label for="exampleInputEmail1">Title</label> 
			<input type="text"
				name='title' class="form-control" placeholder="Enter Title">
		</div>
		<div class="form-group">
			<label for="exampleInputPassword1">Content</label>
			<textarea class="form-control" name="content" rows="3"
				placeholder="Enter ..."></textarea>
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">Writer</label> 
			<input type="text"
				name="writer" class="form-control" placeholder="Enter Writer">
		</div>
	</div>
	<!-- /.box-body -->

	<div class="box-footer">
		<button type="submit" class="btn btn-primary">Submit</button>
	</div>
</form>

 

글작성완료시 새로고침을 하게 되면 POST방식으로 다시 한 번 데이터를 전송할 것인지를 묻는다.

이럴 경우에 계속을 클릭하게 되면 같은 글이 다시 등록되는데(동일한 데이터 재전송) 이것을 방지하기 위해서 redirect:를 사용할 수 있다.

여기서 새로고침후에 URI에 값을 없에주기 위해서는 리다이렉트 시점에 한 번만 사용되는 데이터를 전송할 수 있는 RedirectAttributes의 addFlashAttribute()를 사용할 수 있다.

addFlashAttribute()는 브라우저까지 전송되기는 하지만, URI상에는 보이지 않는 숨겨진 데이터의 형태로 전달된다.

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registPOST(BoardVO board, RedirectAttributes rttr) throws Exception {

  logger.info("regist post ...........");
  logger.info(board.toString());

  service.regist(board);

  rttr.addFlashAttribute("msg", "success");
  return "redirect:/board/listAll";
}