코드로 배우는 스프링 웹 프로젝트를 보고 했습니다.
- register.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <form role="form" method="post"> <div class="boax-body"> <label for="exampleInputEmail"></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> <!-- /.box-body -->
<div class="box-footer"> <button type="submet" class="btn btn-primary">Submit</button> </div> </form>
</body> </html> |
- Success.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <!-- left column --> <div class="col-md-12"> <!-- general form elements -->
<div class="box"> <div class="box-header with-border"> <h3 class="box-title">SUCCESS PAGE</h3> </div> <div class="box-body">SUCCESS!!!!</div> <!-- /.box-body --> <div class="box-footer">Footer</div>
<!-- /.box-footer --> </div> </div> <!-- /.col (left) --> </body> </html> |
BoardController.java
@RequestMapping(value="/read", ..이하 생략) @RequestMapping(value="/remove", ..이하 생략) @RequestMapping(value="/modify", ..이하 생략)는 우선 신경 쓰지 않고 register와 success만 신경 쓰면 된다.
- BoardController.java
package org.zerock.controller;
import javax.inject.Inject;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.servlet.mvc.support.RedirectAttributes; import org.zerock.domain.BoardVO; import org.zerock.services.BoardService;
@Controller public class BoardController {
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
@Inject private BoardService service;
@RequestMapping(value = "/register", method = RequestMethod.GET) public String registerGET(BoardVO board, Model model) throws Exception { logger.info("register get......"); return "/board/register"; }
@RequestMapping(value = "/register", method = RequestMethod.POST) public String registPOST(BoardVO board, Model model) throws Exception {
logger.info("regist post ......"); logger.info(board.toString());
service.regist(board);
model.addAttribute("msg", "success");
return "/board/success"; // return "/redirect:/baord/listAll"; }
@RequestMapping(value = "/listAll", method = RequestMethod.GET) public void listAll(Model model) throws Exception { logger.info("show all list................"); model.addAttribute("list", service.listAll());
}
@RequestMapping(value = "/read", method = RequestMethod.GET) public void read(@RequestParam("bno") int bno, Model model) throws Exception { model.addAttribute(service.read(bno)); }
// 삭제 처리 @RequestMapping(value = "/remove", method = RequestMethod.POST) public String remove(@RequestParam("bno") int bno, RedirectAttributes rttr) throws Exception { service.remove(bno); rttr.addFlashAttribute("msg", "SUCCESS"); return "redirect:/board/listAll"; }
// 수정 @RequestMapping(value = "/modify", method = RequestMethod.GET) public void modifyGET(int bno, Model model) throws Exception { model.addAttribute(service.read(bno)); }
@RequestMapping(value = "/modify", method = RequestMethod.POST) public String modifyPOST(BoardVO board, RedirectAttributes rttr) throws Exception { logger.info("mod post.......");
service.modify(board); rttr.addFlashAttribute("msg", "SUCCESS"); return "redirect:/board/listAll"; } } |
<결과>
title과 content 그리고 writer를 적은 후 submit을 누르면 성공적으로 저장했다는 success페이지가 뜬다.
But 결과 페이지의 문제점 – 새로 고침
위의 상태에서 사용자가 ‘새로 고침’을 클릭하게 되면 브라우저는 POST 방식으로 다시 한 번 데이터를 전송할 것인지를 묻습니다.
위의 화면에서 ‘계속’을 선택하게 되면 새로운 게시글이 정상적으로 등록됩니다. 하지만 악의적인 사용자가 ‘새로 고침’과 ‘계속’을 선택하면 인터넷에서 말하는 ‘도배(동일한 글로 게시물 목록을 차지하는 현상)’라는 일이 벌어집니다. 이 문제를 막기 위해서는 가장 간단하게 사용할 수 있는 방법은 바로 페이지를 다른 곳으로 이동하는 리다이렉트를 이용하는 방법입니다.
목록 즉 사용자가 ‘새로 고침’을 이용하면 자동으로 다른 페이지로 이동하는 작업이 필요 합니다. 이 경우 가장 많이 쓰는 방식이 리다이렉트를 이용하는 방법입니다. 대부분의 경우 등록 작업이 처리되면 자동으로 목록을 조회하는 페이지로 이동하게 됩니다. 등록이 성공한 후에 바로 리다이렉트 결과 페이지로 이동하면 사용자는 POST로 작성된 결과 페이지를 보지 못한 상태에서 바로 목록 페이지를 보게 됩니다.
다음에는 리다이렉트로 목록 페이지로 이동하게 하기!
'JAVA > spring' 카테고리의 다른 글
spring redirection (0) | 2019.08.07 |
---|---|
spring redirect (4) | 2019.07.04 |
spring homapage (0) | 2019.06.14 |
Spring CreateTest (0) | 2019.06.13 |
Spring Connection(DataSource) (0) | 2019.06.11 |
댓글