공부/Spring Boot
뷰(form)에 입력한 많은 데이터를 List 타입으로 컨트롤러로 보내는 방법
오피스엑소더스
2019. 9. 9. 17:12
1. 목표
1. 개발환경
> Spring Boot 2.1.7, JDK 8
2. 뷰에서 많은 양의 데이터를 컨트롤러로 보내고자 한다.
> 리스트 형태로 보낼 수 있다.
2. 기능
1. 리스트 타입으로 보내기 순서
> 데이터가 담길 ExamDTO 작성(getter, setter)
- String title, String subject, String limit_time
> ExamDTO를 List타입으로 갖는 ExamListDTO 작성(getter, setter)
- List<ExamDTO> examlist
> 뷰에서 보낼 데이터를 폼으로 묶음
- action : listtest
- method : post
- input 태그
- name : ExamListDTO에서 선언한 변수명을 사용하여 작성
- examlist[0].title, examlit[0].subject ...
> 컨트롤러에서 데이터를 받음
- @ModelAttribute(value="ExamTestListDTO") ExamTestListDTO l
3. 소스코드
1. ExamTestDTO(모델)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.cbt.app.common; import java.io.Serializable; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class ExamTestDTO implements Serializable{ private String title; private String subject; private String limit_time; } | cs |
2. ExamTestListDTO(모델)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.cbt.app.common; import java.util.List; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class ExamTestListDTO { private List<ExamTestDTO> examlist; } | cs |
3. test.jsp(뷰)
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> private String title; private String subject; private String limit_time; <form action="listtest" method="post"> <input type="text" name="examlist[0].title" value="1번"> <input type="text" name="examlist[0].subject" value="국어"> <input type="text" name="examlist[0].limit_time" value="30"> <input type="text" name="examlist[1].title" value="2번"> <input type="text" name="examlist[1].subject" value="영어"> <input type="text" name="examlist[1].limit_time" value="60"> <input type="text" name="examlist[2].title" value="3번"> <input type="text" name="examlist[2].subject" value="수학"> <input type="text" name="examlist[2].limit_time" value="100"> <input type="submit" value="ggg"> </form> </body> </html> | cs |
4. CommonController(컨트롤러)
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 | package com.cbt.app.common; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class CommonController { @RequestMapping(value = "/welcome") public String welcome() { return "welcome"; } @RequestMapping(value = "/test") public String testView() { return "test"; } @RequestMapping(value = "/listtest" , method = {RequestMethod.POST, RequestMethod.GET}) public String listtest(Model model, @ModelAttribute(value="ExamTestListDTO") ExamTestListDTO l) { System.out.println("리스트 테스트!!! 받았다!!!"); System.out.println(l); return "test"; } } | cs |