출처 : http://warmz.tistory.com/727
1. HttpServletRequest, HttpServletResponse
2. HttpSession
3. Local
- java.util.Locale 지역 정보
4. InputStream, Reader
- HttpServletRequest의 getInputStream(), Reader
5. OutputStream, Writer
- HttpServletResponse의 getOutputStream(), Writer - 서블릿 형태로 만들때 사용한다.
6. @PathVariable
- @RequestMapping의 URL {} 부분의 패스 변수를 받는다.
만약 타입이 틀린 값이 들어오면 HTTP 400 - Bad Request 가 전달 된다.
1 2 | @RequestMapping ( "/board/{id}" ) public void view( @PathVariable ( "id" ) int id ) {...} |
7. @RequestParam
- 스프링 내장 변환기가 다룰 수 있는 모든 타입을 지원한다.
해당 파라미터가 없다면 HTTP 400 - Bad Request 가 전달 된다.
- file의 경우는 <input type="file" name="file" /> 에 매핑 된다.
1 2 3 | public String edit( @RequestParam ( "id" ) int id, @RequestParam ( "title" ) String title, @RequestParam ( "file" ) MultipartFile file ) {...} |
- 맵 형태로 받으면 모든 파라미터 이름은 맵의 키에 파라미터 값은 맵의 값에 담긴다.
1 | public String add( @RequestParam Map<String, String> params ) {...} |
- 파라미터가 필수가 아니라면 required = false 로 지정하면 된다.
1 2 3 | public void view( @RequestParam (value = "id" , required = false , defaultValue = "0" ) int id) {..}. |
8. @CookieValue
- @RequestParam과 동일 하며 쿠키값을 가져올 때 사용한다.
1 2 | public String check( @CookieValue ( "check" ) String check, required = false , defaultValue = "" ) {...} |
9. @RequestHeader
- 헤더 정보를 메소드 파라미터에 넣어 준다. Ajax로 처리할때 $.ajax(...) 에서 head에 특정 값을 넣고 여기서 받아서
있으면 ajax이고 없으면 일반페이지라는 식으로 이용하면 된다.
1 | public String header( @RrequestHeader ( "ajax" ) String ajax ) {...} |
10. Map, Model, ModelMap
- view를 String으로 리턴해 주고 Attribute를 Map, Model, ModelMap 에 담을 수 있다.
11. @ModelAttribute
- 파라미터를 Object형태로 받을때 사용된다. 일반적인 파라미터 형태로 쓰인 경우 타입이 일치하지 않으면 객체에 매핑 되지
않으며 에러는 발생 시키지 않는다. 자동으로 ModelMap에 담기므로 modelMap.addAttribute를 해 줄 필요가 없다.
1 | public void update( @ModelAttribute ( "board" ) Board board) {...} |
- 메소드에도 @ModelAttribute를 설정 할 수 있다. 리턴값이 항상 나머지 컨트롤러에 자동 추가 되며 보통 참조용 데이터 등에
이용된다.
1 2 | @ModelAttribute ( "emailList" ) public Map<String, String> getEmailList() { ... } |
12. Errors, BindingResult
- 모델의 값을 검정한다. 이때 BindingResult나 Errors의 파라미터 값의 위치는 반드시 @ModelAttribute 뒤에 위치해야 한다.
자신의 바로 앞에 있는 @ModelAttribute 파라미터의 검정 작업만 하기 때문이다.
1 2 | @RequestMapping (value = "/board/add" , method = RequestMethod.POST) public String add( @ModelAttribute ( "board" ) Board board, BindingResult result ) {...} |
13. SessionStatus
- 모델 오브젝트를 세션에 저장하여 계속 사용한다. 더이상 모델 오브젝트를 사용하지 않을 때는 세션에서 제거해 줘야 한다.
14. @RequestBody
- HTTP body 부분만 전달 한다. XML 이나 JSON 으로 출력 할 경우 사용한다.
리턴타입의 @ResponseBody 를 참조하자.
15. @Value
- 프로퍼티값이나 값을 파라미터에 적용한다.
1 2 3 4 5 6 7 8 9 | public class BoardController { @Value ( "${eng.url}" ) String engUrl; @RequestMapping (..) public String gotoEng() { return this .engUrl; } } |
1 2 3 | public String gotoEng( @Value ( "${eng.url}" ) String engUrl ) { return engUrl; } |
16. @Valid
- JSR - 303 검증기를 이용해서 @ModelAttribute를 검정하도록 한다.
1 | public String add( @Valid @ModelAttribute ( "board" ) Board board, BindingResult result ) { ...} |
'Java > Spring' 카테고리의 다른 글
Jackson ObjectMapper에서 json data를 Map이 아니라 Object로 받기 (0) | 2018.09.12 |
---|---|
[삽질] stomp, sockjs를 이용하여 websocket 연결 시 info 가 404로 나오는 경우 (4) | 2018.03.13 |
이미지 파일 업로드 오류 수정 기록... (0) | 2016.11.08 |
Spring Batch(스프링 배치)에서 작업 시간이 길어지는 경우 (0) | 2016.10.27 |
Spring에서 MessageUtil 사용하기 (0) | 2015.02.24 |