@RequestParam 어노테이션은 HttpServletRequest 객체와 같은 역할
① HttpServletRequest 의 getParameter()메소드를 이용하는 것과
② RequestParam을 통해 값을 받아오는 것은 같은 방법
@RequestParam("가져올 데이터의 이름") [데이터 타입][가져온 데이터를 담을 변수]
-> Model객체를 이용해서 뷰로 값을 넘겨준다
- @RequestParam은 1개의 HTTP 요청 파라미터를 받기 위해서 사용.
- 필수 여부가 true이기 때문에 기본적으로 반드시 해당 파라미터가 전송되어야 한다. 필수가 아니면 required=false를 추가할 수 있음
- 해당 Parameter를 사용하지 않고 요청을 보낼 경우에 default로 받을 값을 defaultValue 옵션을 통해 설정 가능
@GetMapping("/list")
public ResponseEntity<List<Board>> requestParam(
@RequestParam(value = "searchKeyWord1", required = false) final String searchKeyWord1,
@RequestParam(value = "writer", defaultValue = "MangKyu") final String searchKeyWord2) {
// searchKeyWord1는 required가 false이기 때문에 없을 수도 있다. final List<Board> boardList = searchKeyWord1 != null
? boardService.getBoardList(searchKeyWord1)
: boardService.getBoardList();
// 반면에, searchKeyWord2는 required가 true이기 때문에 반드시 요청 파라미터로 존재해야 한다.
log.info(searchKeyWord2);
return ResponseEntity.ok(boardList);
}
@RequestParam
- 요청 매개변수에 들어있는 기본 타입 데이터를 메서드 인자로 받아올 수 있음
- 요청 매개변수랑 URL 요청의 localhost:8080?username=value&age=value의 쿼리 매개변수과 HTML 태그의 Form 데이터가 해당
- HTTP Method GET일 때는 Content-Type필요없음
- HTTP GET으로 요청을 보내면 무조건 URL 끝에 쿼리스트링으로 key=value 형식으로 날아가기 때문에 굳이 Content-Type 헤더가 필요 없다.
@RequestMapping(value = "/doOneFileDownload")
public void doOneFileDownload(HttpServletResponse response,
@RequestParam(value = "filepath", defaultValue = "") String filePath,
@RequestParam(value = "filename", defaultValue = "") String fileName) {
try {
fileName = URLDecoder.decode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
ResponseDownloadHelper.downloadFile(response, filePath, fileName);
}
[RequestParam]
https://hongku.tistory.com/119
https://mangkyu.tistory.com/72
https://amagrammer91.tistory.com/106