[Spring] redirect

세상을 바꾸는 개발자·2023년 3월 22일
0

redirect

@PostMapping("/create/{id}")
public String createAnswer(Model model, @PathVariable Integer id, @RequestParam String content) {
    
    Question question = questionService.getQuestion(id);

    return "redirect:/question/detail/%d".formatted(id);
}

  • 직원1과 직원2는 소통이 안됨
  • 고객이 직원1한테 요청을 했는데, 그 요청이 직원2와 관련되어있어서 직원2한테 가라고 전달 -> 요청이 2번 일어남


redirect하지 않고 본인이 직접 실행

@PostMapping("/create/{id}")
public String createAnswer(Model model, @PathVariable Integer id, @RequestParam String content) {
    // 관련 질문을 얻어온다.
    Question question = questionService.getQuestion(id);
    
    // TODO : 답변객체를 만든다.
    
    model.addAttribute("question", question);

    return "question_detail"; //고객이 다시 GET 요청할 필요가 없어진다. 
}

  • 직원1과 직원2는 소통이 됨
  • 고객이 직원1한테 요청을 했는데, 그 요청이 직원2와 관련되어 있어도 직원1이 직원2의 내용을 가져와서 수행함 -> 요청이 1번 일어남
  • redirect:<URL> - URL로 리다이렉트 (리다이렉트는 완전히 새로운 URL로 요청이 된다.)
profile
초심 잃지 않기

0개의 댓글