반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- DROP
- WriteLine
- c#상속
- 메소드 지정자
- MariaDB
- ForignKey
- 타입이 서로 다른 두 데이터 제네릭
- 평면좌표상에서 두점 거리 구하기
- final
- @ Builder
- unity 오브젝트
- response
- Create
- 요청
- java
- JDBC
- select
- static
- static을 왜사용할까?
- unity 레이아웃
- 메소드 정의
- request
- http
- db
- unity 간단 설정
- C#
- spring
- 데이터베이스
- Database
- 타입 변수 표기법
Archives
- Today
- Total
이론을 싫어!
@GetMapping , @PostMapping 에 대해 알아봅시다! 본문
반응형
@GetMapping 과 @PostMapping 은 Get 방식으로 요청이 오는지 아니면
Post방식으로 요청이 오는지에 따라서 정해준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@Controller
public class RegisterController{
// @RequestMapping(value="/register/add" , RequestMethod.GET) 아래 @GetMapping와 같다.
@GetMapping("/register/add")
public String register(){
return "registerForm";
}
// @RequestMapping(value="/register/add" , RequestMethod.POST) 아래 @PostMapping와 같다.
@PostMapping("/register/add")
public String save(User user, Model m) throws Exception{
if(!isValid(user)){
String msg=URLEncoder.encode("id를 잘못입력하셨습니다." , "utf-8");
m.addAttribute("msg",msg);
return "redirect:/register/add"; // redirect는 재요청의 의미를 가지고 있다.
}
return "registerInfo";
}
|
cs |
@GetMapping 이랑 @PostMapping 쓰는 법이다.
물론 @RequestMapping 으로 써도 되지만 @GetMapping 또는 @PostMapping 으로 쓰는것이 더 간단 하다.
위의 코드를 좀 설명을 좀 하면
registerForm 의 메소드는 get방식으로 요청이 들어왔을때 처리하는 메소드 이며,
save의 메소드는 Post 방식으로 요청이 들어왔을떄 처리하는 메소드이다.
우리가 GetMapping 이랑 PostMapping의 방식을 배우기 전에는 URL 같으면 충돌 나지만
Get 과 Post의 방식은 다르기 때문에 충돌이 나지 않는다 .
그러면 이제 RequestMapping 은 안쓰나??
꼭 그렇지 않다. 밑의 코드를 보자
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Controller
@RequestMapping("/register")
public class RegisterController{
// @RequestMapping(value="/register/add" , RequestMethod.GET) 아래 @GetMapping와 같다.
@GetMapping("/add")
public String register(){
return "registerForm";
}
// @RequestMapping(value="/register/add" , RequestMethod.POST) 아래 @PostMapping와 같다.
@PostMapping("/add")
public String save(User user, Model m) throws Exception{
if(!isValid(user)){
String msg=URLEncoder.encode("id를 잘못입력하셨습니다." , "utf-8");
m.addAttribute("msg",msg);
return "redirect:/register/add";
}
return "registerInfo";
}
|
cs |
RequestMapping으로 중복된 부분을 클래스에 적용시켜준 부분이다.
즉, 클라이언트에서 http://localhost:8080/ch2/register 라고 요청이 들어오면 위의 클래스에 요청이 들어온것이다.
'Spring and Springboot' 카테고리의 다른 글
Lombok 빨간색 고추모양??? 에 대해 알아봅시당 (0) | 2023.03.28 |
---|---|
[Spring] redirect와 forward 둘중 어떤걸 써야될까?? (0) | 2023.03.14 |
HTTP 요청과 응답 GET, POST 그리고 실행 결과까지 (0) | 2023.03.09 |
[Spring]웹 애플리케이션 서버(WAS)에 대한 정보 알아보기! (Tomcat 내부 구조 ) (0) | 2023.03.07 |
[Spring] Http 예제 부분 응용 (0) | 2023.03.06 |