실전 개발자를 위한 Spring Framework Day9 (2회차)
HTTP 파라미터 처리
1. 파라미터 전송받기(RequestParam)
HTTP 요청 파라미터
@RequestMapping("/url")
public ModelAndView example(@RequestParam("key") String var){
....
}
- get/post로 요청을 넘겨 받음
@RequestMapping("/url")
public ModelAndView example(@RequestParam(value = "key", required = false, defaultValue="default") String var){
....
}
- required = false는 요청 값이 없을 때 에러를 보내지 않고 null을 리턴
- defaultValue가 설정돼 있으면 null값 대신 설정 값을 리턴
2. Command 객체를 이용한 폼 전송
@RequestMapping("/url")
public ModelAndView example(MemberVO memberVO)){
....
}
//MemberVO의 멤버변수명과 파라미터로 넘어온 키 값이 일치하면 memberVO객체 멤버변수에 값에 자동 입력
- Command 객체는 View의 Model로 자동 등록된다
view.addObject("memverVO", memverVO)와 같은 코드가 불필요
- 커맨드 객체를 jsp에서 사용할 때는 객체명 중 첫 글자만 소문자로 바꾸어 사용
- 타입이 자동으로 변환됨
- 숫자는 int(long), double 타입으로 변환 가능
- true, false는 boolean 타입으로 변환 가능
- 같은 name이 여러개 넘어오면 List로 처리
<!--jsp-->
<input type="checkbox" name="isCheck" value="true"/>
<input type="text" name="people" value="홍길동"/>
<input type="text" name="people" value="임꺽정"/>
<input type="text" name="people" value="강감찬"/>
//command객체
private boolean isCheck;
private List<String> people;