데이터 전달 및 Param 값 받기
- UI 템플릿으로 데이터 전달 방법 4가지 (타임리프, JSP, 머스테치)
- request 객체, session 객체
- GET, POST 파라미터
-
@RequestMapping("/model2") public String model2(HttpServletRequest request){ String param_name = request.getParameter("name"); String param_age = request.getParameter("age"); request.setAttribute("name", param_name); request.setAttribute("age", param_age); return "index"; }
-
- Model 객체
- ModelAndView 객체
- 모델(데이터)와 뷰(HTML)을 동시에 저장 및 응답하는 객체
- 예시
-
@RequestMapping("model4") public ModelAndView model3(HttpServletRequest request, ModelAndView mv) { mv.addObject("name", "춘향이"); mv.addObject("age", "18"); //뷰 설정 mv.setViewName("index"); return mv; }
-
- Param 값 받는 4가지 방법
- 먼저 html에서 submit으로 사용자에게 입력값 받기
- 예시
-
<form action="이동할url명" method="post"> <input type="text" name="username" > <br> <input type="password" name="password" > <br> <input type="submit" value="로그인"> </form>
- request.getParameter() 사용
- 메서드의 매개변수는 HTTPServletRequest request, Model model, 클래스명 변수명을 받는다.
- 변수 선언 후 html에서 값을 받았던 name을 request.getParameter(”name”) 형태로 받아온다.
- 해당 변수를 model.addAttribute(”attributeName”, 변수명) 형태로 추가한다.
- 예시
-
@RequestMapping("loginAction1") // 1. 메서드의 매개변수는 HTTPServletRequest request, Model model, 클래스명 변수명을 받는다. public String loginAction1(HttpServletRequest request, Model model, Member member) { // 2. 변수 선언 후 html에서 값을 받았던 name을 request.getParameter(”name”) 형태로 받아온다. String username = request.getParameter("username"); String password = request.getParameter("password"); // 3. 해당 변수를 model.addAttribute(”attributeName”, 변수명) 형태로 추가한다. model.addAttribute("username", username); model.addAttribute("password", password); return "loginResult"; }
-
- @PathVariable 사용
- 메서드의 매개변수에서 @PathVariable(”html에서받았던name”) 데이터타입 변수명 형태로 준다.
- 매개변수는 위의 @PathVariable, Model model, 클래스명 변수명을 받는다.
- 해당 매개변수명을 model.addAttribute(”attributeName”, 매개변수명) 형태로 추가한다.
- 예시
-
@RequestMapping("loginAction1/{username}/{password}") // 1. 메서드의 매개변수에서 @PathVariable(”html에서받았던name”) 데이터타입 변수명 형태로 준다. // 2. 매개변수는 위의 @PathVariable, Model model, 클래스명 변수명을 받는다. public String loginAction1( @PathVariable("username") String username, @PathVariable("password") String password, Model model, Member member) { // 3. 해당 매개변수명을 model.addAttribute(”attributeName”, 매개변수명) 형태로 추가한다. model.addAttribute("username", username); model.addAttribute("password", password); return "loginResult"; }
-
- @RequestParam 사용
- 메서드의 매개변수에서 @RequestParam(”html에서받았던name”)와 Model model을 받는다.
- RequestParam은 required로 null 여부와 defaultValue로 기본값을 설정할 수 있다.
- 위에서 추가한 매개변수명 model.addAttribute(”attributeName”, 매개변수명) 형태로 추가한다.
- 예시
-
@RequestMapping("loginAction2") // 1. 메서드의 매개변수에서 @RequestParam(”html에서받았던name”)와 Model model을 받는다. // 2. RequestParam은 required로 null 여부와 defaultValue로 기본값을 설정할 수 있다. public String loginAction2( @RequestParam(value="username", required = false, defaultValue = "김진사") String username, @RequestParam("password") String password, Model model){ // 3. 위에서 추가한 매개변수명 model.addAttribute(”attributeName”, 매개변수명) 형태로 추가한다. model.addAttribute("username", username); model.addAttribute("password", password); return "loginResult"; }
-
- Map 사용
- @RequestParam으로 Map을 선언하고, Model model을 매개변수로 받는다.
- 선언한 map을 model.addAttribute로 추가한다.
- 예시
-
@RequestMapping("loginAction4") // 1. @RequestParam으로 Map을 선언하고, Model model을 매개변수로 받는다. public String loginAction5( @RequestParam Map<String, Object> map, Model model){ // 2. 선언한 map을 model.addAttribute로 추가한다. model.addAttribute("map", map); return "loginResult3"; }
-
- 먼저 html에서 submit으로 사용자에게 입력값 받기
- required
- required = true
- Nonnull이 기본값 (null이면 오류)
- required = false
- 매개변수로 null 허용
- 예시 (@RequestParam 매개변수에 사용)
- @RequestParam(value="username", **required = false**, defaultValue = "김진사") String username, …
- required = true
- defaultValue
- 매개변수의 값이 없을 때 기본값 지정
- 예시
- @RequestParam(defaultValue = "홍길동")
- ArrayList 사용하여 데이터 저장
- ArrayList 선언
- 메서드 안에 클래스객체 new 인스턴스로 생성한다.
- 해당 클래스객체.setter변수(매개변수명); 형태로 클래스객체에 값을 넣는다.
- 이후 List에 클래스 객체 전체를 add 한다.
- 예시
-
// 1. ArrayList 선언 List<Member> memberList = new ArrayList<>(); @PostMapping("join") public String join(@RequestParam(defaultValue = "홍길동", required = false) String inputName, @RequestParam String inputEmail, @RequestParam String inputPw, @RequestParam String inputPw2, Model model){ // 2. 메서드 안에 클래스객체 new 인스턴스로 생성한다. Member member = new Member(); // 3. 해당 클래스객체.setter변수(매개변수명); 형태로 클래스객체에 값을 넣는다. member.setName(inputName); member.setEmail(inputEmail); member.setPassword(inputPw); // 4. 이후 List에 클래스 객체 전체를 add 한다. memberList.add(member); return "index"; }
-
728x90
'Back-end > Spring Boot' 카테고리의 다른 글
[Spring Boot] Devtools, LocaleResolver(다국어 처리) (0) | 2024.04.03 |
---|---|
[Spring Boot] builder, Rest API (0) | 2024.03.31 |
[Spring Boot] Thymeleaf, redirect/forward (0) | 2024.03.31 |
[Spring Boot] Request Mapping의 종류, @RequestParam (0) | 2024.03.31 |
[Spring Boot] Lombok 어노테이션 종류 (0) | 2024.03.31 |