ENN
[Spring] "redirct:/"와 "view이름" retrun의 차이점 본문
Spring Controller는 URL 요청을 다양한 방법으로 처리한다.
Redirect?
redirect는 웹 브라우저(사용자)가 어떤 URL로 웹 서버를 요청했을 때 다른 URL로 넘겨주는 것을 말한다.
예를 들어, Gmail로 접속했을 때 로그인이 되어 있지 않다면 로그인이 선행되어야 하기 때문에 로그인 페이지로 이동시키는 것을 redirect라고 한다.
redirect:의 다음 문자열이 "/"로 시작하면 웹 어플리케이션을 기준으로 이동 경로를 생성하고, 그렇지 않으면 현재 경로를 기준으로 한 상대 경로를 사용한다.
...
@Controller
public class HomeController{
@GetMapping(path="/")
public String hello(){
System.ou.println("1.Call hello()");
return "home";
}
@GetMapping(path="/book")
public String showBook(){
System.out.println("2.Call showBook()");
return "home";
}
@GetMapping(path="/coffee")
public String showCoffee(){
System.out.println("3.Call showCoffee()");
return "redirect:/";
}
}
1. return "view이름"
/book 요청 시 콘솔창에 2.Call showBook() 로그가 출력된다.
주소는 localhost:8080/book으로 변동되었고 home.html가 보여진다.
2. return "redirect:/..."
/coffee 요청 시
콘솔창에 3.Call showCoffee()가 출력된 후
return redirect:/가 있기 때문에 다시 /(root, 즉 localhost:8080)를 호출하고
그에 따라 hello()를 한번 더 호출하여 1.Call hello()가 출력된다.
따라서 주소는 localhost:8080
<정리>
return "viewName"는 viewName에 해당하는 view를 보여준다.
return "redirect:/"는 redirect 후에 적힌 주소로 URL 요청을 다시 한다.
참고)
https://blog.naver.com/PostView.nhn?isHttpsRedirect=true&blogId=sim4858&logNo=221007278858
'백엔드' 카테고리의 다른 글
[Spring boot] Spring project의 폴더 구조 (0) | 2022.09.19 |
---|---|
[MySQL] 데이터베이스 컬럼 데이터 타입 선택하기 (0) | 2022.09.19 |
쿠키(Cookie)와 세션(Session) (0) | 2022.09.19 |
My Batis (0) | 2022.09.19 |
MVC MVVM MVP (0) | 2022.09.19 |