MVC_Spring - @RequestParam 사용하기
프로그래밍 정리/Spring
2019. 12. 31. 15:39
반응형
MVC_Spring - @RequestParam 사용하기
- 직접 받는 방법은 3가지가 있다.
첫번째 - HttpServletRequest방식
메서드( HttpServletRequest httpservletRequest )
-model과 같이 메소드에 써두면 값이 자동으로 넣어진다.
1 2 3 4 5 6 7 8 | @RequestMapping("board/confirmId") public String confirmId(HttpServletRequest httpServletRequest, Model model) { String id = httpServletRequest.getParameter("id"); //httpServletRequest에 들어있는 값을 새로운 String에 넣는다. String pw = httpServletRequest.getParameter("pw"); //httpServletRequest에 들어있는 값을 새로운 String에 넣는다. model.addAttribute("id", id); //httpServletRequest에서 가져온 값을 model에 넣는다. model.addAttribute("pw", pw); //httpServletRequest에서 가져온 값을 model에 넣는다. return "board/confirmId"; } | cs |
-board/confirm.jsp
1 2 3 4 5 6 7 8 9 10 11 | <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> ID : ${id} <br /> PW : ${pw} </body> </html> | cs |
※만약 값을 안넣고 요청이 들어와도, 실행은 된다.
값을 안넣었을때
값을 넣었을때
두번째 - @RequestParam 방식
메소드( @RequestParam("name") Type type_name )
-코드가 짧아지는 장점이 있다.
1 2 3 4 5 6 | @RequestMapping("board/checkId") public String checkId(@RequestParam("id") String id, @RequestParam("pw") int pw, Model model) { //여러개의 값들을 받을 수 있다. model.addAttribute("identify", id); model.addAttribute("password", pw); return "board/checkId"; } | cs |
-board/checkId.jsp
1 2 3 4 5 6 7 8 9 10 11 | <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> ID : ${identify} <br /> PW : ${password} </body> </html> | cs |
※만약 값을 안넣고 요청이 들어오면, 실행이 안된다.
값을 안넣었을때
값을 넣었을때
세번째 - 객체 방식
메소드(객체 생성)
1 2 3 4 5 | @RequestMapping("/member/join") public String joinData(Member member) { return "member/join"; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Member { private String id; private String pw; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } } | cs |
※자동으로 setter를 이용하여 값이 들어간다.
값을 안넣었을때
값을 넣었을때
--------------------------모든 소스----------------------------------
-controller.java
|
-board/checkId.jsp
1 2 3 4 5 6 7 8 9 10 | <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> ID : ${identify} <br /> PW : ${password} </body> </html> | cs |
-board/confirm.jsp
1 2 3 4 5 6 7 8 9 10 | <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> ID : ${id} <br /> PW : ${pw} </body> </html> | cs |
-member/join.jsp
1 2 3 4 5 6 7 8 9 10 | <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> 아이디 : ${member.id} <br /> 비밀번호 : ${member.pw} <br /> </body> </html> | cs |
반응형
'프로그래밍 정리 > Spring' 카테고리의 다른 글
MVC_Spring - Redirect 사용하기 (0) | 2019.12.31 |
---|---|
MVC_Spring - @ModelAttribute 사용하기 (0) | 2019.12.31 |
MVC_Spring - @RequestMapping 사용하기 (0) | 2019.12.31 |
MVC_Spring - MVC_Spring 시작 (0) | 2019.12.31 |
Spring - Aop autoproxy방식,pointcut 종류 (0) | 2019.12.31 |