Spring Presentation Layer

[Spring MVC(Model-View-Controller)의 핵심 Component]

참고 https://www.egovframe.go.kr/wiki/doku.php?id=egovframework:rte:ptl:spring_mvc_architecture
Spring MVC 란?
1. Client Request
2. DispatcherServelet : 최초 진입 지점
3. Handler Mapping : 어떤 컨트롤러에게 요청을 위임하면 좋을지 판단.
해당 요청을 처리하기 위한 Controller 찾음.
4. DispatcherServelet: 선태된 Controller호출하여 1. Client Request처리
5. Controller : Business Layer와 통신을 통해 원하는 작업 처리 후 요청에 대한 성공유무에 따라 ModelAndView인스턴스 반환.
6. DispatcherServelet : ModelAndView의 View이름이 논리적인 정보이면 ViewResolver 참조해 처리할 View생성
7. DispatcherServelet : ViewResolver 통해 View에게 ModelAndView전달.
8. View : 클라이언트에게 UI 제공
Annotation 설명
@RestController ( = @Controller + @ResponseBody)
: Rest를 위한 Controller 기능을 부여하는 Annotation. 반환값을 JSON으로 변환해준다.
@Controller
: 템플릿을 이요해 HTML페이지를 렌더링 및 표시해줌
@ResponseBody
: 반환값을 JSON으로 변환해줌
@GetMapping
: Get메소드 Controller Annotation. 리소스를 조회하는 요청에 사용
EX) @GetMapping("") : "" 에서 url mapping 지정
@RestController
public class testController {
@GetMapping("")
public String hello(){
return "hello world";
}
}
@RequestMapping
EX) @RequestMapping (method = RequestMethod.Get, value="") : Get method이면서 url mapping은 "" 의미.
@RestController
public class testController {
@RequestMapping (method = RequestMethod.Get, value="")
public String hello(){
return "hello world";
}
}
Controller자체에 URL Mapping도 가능
@RestController
@RequestMapping("test")
public class testController{
}
@PathVariable : URL경로에 변수를 넣는 방식. RESTful API에서 사용함.
@RestController
public class testController{
@GetMapping("/student/{name}")
public String getName(@PathVariable(value = "name") final String name){
return name;
}
}
/{name} : {name}을 path에서 name이라는 Parameter로 받으라는 것을 의미.
/{name} -> @PathVariable(value = "name") -> String name으로 변환 -> name 반환
@RequestParam : URL 파라미터로 값을 넘기는 방식
@RestController
public class StudentController{
//ex1
@GetMapping("/student/department")
public String getMajor(@RequestParam(value="department", defaultValue="") final String department){
return department;
}
//ex2
@GetMapping("/student/info")
public String getMajor(@RequestParam(value="name") final String name)
(@RequestParam(value="address") final String address){
return name + "의 주소는 " + address + "입니다.";
}
//ex3
@GetMapping("/student/score")
public String getMajor(@RequestParam(value="score") final int[] score){
int sum = 0;
for(int i : score){
sum += i;
}
return sum;
}
}
@PathVariable VS @RequestParam
@PathVariable : parameter
:/student/{name}
localhost:8080/student/name/최예원
@RequestParam : queryString
: @GetMapping("/student/department")
localhost:8080/student/department?department=컴퓨터학과
-path이란? (url or uri) / (가변라우팅)
ex) localhost:8080/student/name/info : name해당 학생의 정보 조회
localhost:8080/student/list: 모든 학생 리스트 조회
-queryString이란? (uri)
localhost:8080/student/department?department=컴퓨터학과
에서 ?department=컴퓨터학과 부분.
@RequestBody
: Parameter로 객체를 받는 Annotaion
@PostMapping
: Post메소드 Controller Annotation. 리소스 생성 요청에 사용.
@PutMapping
: Put메소드 Controller Annotation. 리소스 수정 요청에 사용.
@DeleteMapping
: Delete메소드 Controller Annotation. 리소스 삭제 요청에 사용.