티스토리 뷰
@ModelAttribute는 @RequestParam과 비슷한데 1:1로 parameter로 받을 경우 @RequestParam을 사용하고 DTO/VO로 받을 경우
@ModelAttribute로 받을 수 있다. validation 작업을 추가로 할 수 있다.
-
@ModelAttribute
-
객체로 받을 수 있다.
-
@RequestParam
-
쿼리스트링을 하나씩 바인딩 받을 수 있다.
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/insert")
public Object insertUser(@ModelAttribute("findUser") User user, BindingResult bindingResult){
if(bindingResult.hasErrors()){
log.info("binding error");
return "invalidParam";
}
if(user.getId() == 1){
user.setLastName("yoon");
user.setAge(28);
user.setLevel(Level.BASIC);
return user;
}
return null;
}
@GetMapping("/insert2")
public Object insertUser(@RequestParam int id, @RequestParam String firstName, @RequestParam String lastName){
return "RequestParamBinding";
}
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
class User{
private int id;
private String firstName;
private String lastName;
private int age;
private Level level;
}
출처: https://coding-start.tistory.com/269 [코딩스타트]
'spring' 카테고리의 다른 글
ModelAndView와 String 리턴 (0) | 2020.06.02 |
---|---|
파일처리는 service에서 해야 할까 controller에서 해야 할까 (0) | 2020.06.02 |
[Spring] Annotation @Bean, @Component (0) | 2020.05.25 |
[Spring] Request, Response와 Annotation (0) | 2020.05.22 |
The dependencies of some of the beans in the application context form a cycle: 오류 (0) | 2020.05.19 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 알고리즘
- 세마포어
- Stack
- 스텍
- 최단경로
- client side rendering
- 구조체
- javascript
- 동적프로그래밍
- 입출력장치
- 이진탐색
- 인접행렬
- 자료구조
- 인접리스트
- 배열
- react
- 소프트웨어
- C++
- stackframe
- server side rendering
- 교착상태
- Java
- 재귀함수
- BFS
- 병행프로세스
- dfs
- 클래스
- 퀵정렬
- C
- 운영체제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함