728x90
@Requestmapping
속성 | 타입 | 설명 |
value | String | 기본 매핑 경로 이름 |
method | RequestMethod | 매핑할 HTTP 요청 방식(GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE) |
headers | String | 매핑된 요청의 헤더 |
name | String | 해당 매핑에 이름 지정 |
params | String | 매핑된 요청 매개변수 |
path | String | 서블릿 환경에서만 경로 매핑 URL |
consumes | String | 매핑된 요청의 소비 가능한 미디어 유형 |
produces | String | 매핑된 요청의 생산 가능한 미디어 유형 |
ReqeustMapping
URL에 매핑되어 있는 것을 처리하기 위해 Method를 사용함
@RequestMapping(value="/exam02", method=RequestMethod.GET)
예제 코드
package com.springmvc.chap05;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/exam02", method=RequestMethod.GET)
//@RequestMapping("/exam02")도 가능
public class example02Controller {
@RequestMapping
public void requestMethod() {
System.out.println("RequestMapping 예제임");
System.out.println("웹 요청 URL은 /exam02 입니다.");
}
}
원래 controller 매핑 하는 방법
package com.springmvc.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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 com.springmvc.domain.Book;
import com.springmvc.service.BookService;
@Controller //클라이언트에서 요청이 오면 처리역할을 담당
@RequestMapping("/books")
public class BookController {
@Autowired // setter를 하지 않아도 외부 컴포넌트를 불러올 수 있음
private BookService bookservice; // 저장소 객체인 BookRepository로 직접 접근하지 않고 서비스 객체로 저장소에 접근 -> 데이터베이스와 직접 연결 X
@RequestMapping
public String requestBookList(Model model) {
List<Book> list= bookservice.getAllBookList(); // getAllBookList()를 가지고 옴
model.addAttribute("bookList", list); // 모델에 booklist라는 키에 booklist를 저장
return "books"; // view 이름, books.jsp
}
}
/books라는 URL 내용이 있으면
해당 @RequestMapping을 보여줌
모델을 생성해서 해당 모델에 booklist를 저장하고
addAttribute로 데이터를 저장함
books라는 웹 view, jsp파일을 보여줌
reqeustmethod.GET : 기본으로 적용되어 있으니까 생략 가능
GET : 조회, 데이터를 받기 위해서 사용
POST : 데이터를 처리, 요청 기능이 추가됨
GET이 기본으로 되어 있기 때문에 POST일 때에만 추가됨
5.3.5. 메소드 수준의 @Reqeustmapping 단순화
애너테이션 | 설명 | method 속성 사용 |
@GetMapping | 매핑할 HTTP 요청 방식이 GET인 경우 | method = RequestMethod.GET |
@PostMapping | 매핑할 HTTP 요청 방식이 POST인 경우 | method = RequestMethod.POST |
@PutMapping | 매핑할 HTTP 요청 방식이 PUT인 경우 | method = RequestMethod.PUT |
@DeleteMapping | 매핑할 HTTP 요청 방식이 DELETE인 경우 | method = RequestMethod.DELETE |
@PatchMapping | 매핑할 HTTP 요청 방식이 PATCH인 경우 | method = RequestMethod.PATCH |
@GetMapping : GET방식으로 매핑해주기
-> @RequestMapping은 여러가지 타입으로 매핑 할 수 있음
package com.springmvc.chap05;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/home")
public class Example04Controller {
@GetMapping("/exam04")
public void requestMethod() {
System.out.println("@RequestMapping 예제입니다.");
System.out.println("웹 요청 URL은 /home/exam04 입니다.");
}
}
728x90
'백 > Spring, Spring Boot' 카테고리의 다른 글
[Spring] 경로 변수(path variables) @PathVariable, @RequestParam (0) | 2023.07.04 |
---|---|
[Spring] 한글 깨짐 방지 (0) | 2023.07.04 |
[Spring] JAVA 애노테이션 Annotation(@) 알아보기 (0) | 2023.06.27 |
[Spring] 웹 애플리케이션 계층 구조 (0) | 2023.06.27 |
[Spring] 스프링(Spring)이란? (0) | 2023.06.21 |