코배스 Part2 : chapter 06
2021. 3. 26. 12:49ㆍSpring/Spring[Info]
반응형
💬 @Controller
파라미터가 자동으로 수집되는 기능!
- 매번 request.getParameter("~~")를 이용하는 불편함을 없앨 수 있다.
- 파라미터를 수집할 때 자동으로 타입을 변환해서 처리한다.
- 자동으로 Bean으로 등록된다.
📕 파라미터의 수집
@Controller
@RequestMapping("/sample/*")
@Log4j
public class SampleController {
// 여러개의 Method 요청을 받고 싶을 때
// http://localhost:8080/sample/basic
@RequestMapping(value = "/basic", method = { RequestMethod.GET, RequestMethod.POST })
public void basicGet() {
log.info("basic...................");
}
// 한개의 Method 요청을 받을 때
// http://localhost:8080/sample/basicOnlyGet
@GetMapping("/basicOnlyGet")
public void basicGet2() {
log.info("basic get only get.........");
}
// DTO 변수를 자동으로 채워줌
// - 이전 JSP/Servlet의 request.getParameter()를 사용하지 않아도 됨
// - 자동으로 형을 맞춰 줌
// http://localhost:8080/sample/ex01?name=AAA&age=11
@GetMapping("/ex01")
public String ex01(SampleDTO dto) {
log.info("" + dto);
return "ex01";
}
// - Parameter를 원하는 형으로 정하고 싶을 때
// - Parameter의 이름과 DTO의 변수명이 다를 때
// http://localhost:8080/sample/ex02?name=AAA&age=11
@GetMapping("/ex02")
public String ex02(@RequestParam("name") String name, @RequestParam("age") int age) {
log.info("name: " + name);
log.info("age: " + age);
return "ex02";
}
// 리스트 처리
// - 동일한 이름의 파라미터가 있을 경우
// http://localhost:8080/sample/ex02List?ids=111&ids=222&ids=333&ids=111
@GetMapping("/ex02List")
public String ex02List(@RequestParam("ids") ArrayList<String> ids) {
log.info("ids: " + ids);
return "ex02List";
}
// 배열 처리
// - 리스트와 동일
// http://localhost:8080/sample/ex02Array?ids=111&ids=222&ids=333&ids=111
@GetMapping("/ex02Array")
public String ex02Array(@RequestParam("ids") String[] ids) {
log.info("ids: " + Arrays.toString(ids));
return "ex02Array";
}
// DTO 객체의 리스트 처리
// - 여러개의 DTO객체를 처리할 수 있다.
// - [ = %5B
// - ] = %5D
// http://localhost:8080/sample/ex02Bean?list%5B0%5D.name=aaa&list%5B1%5D.name=bbb&list%5B2%5D.name=ccc&
@GetMapping("/ex02Bean")
public String ex02Bean(SampleDTOList list) {
log.info("list dtos: " + list);
return "ex02Bean";
}
// ====================================Binding=============================//
// 파라미터를 받을 때 최초 실행 됨
// @InitBinder
// - DTO의 @DateTimeFormat 설정이 없는 경우 사용
public void initBinder(WebDataBinder binder) {
System.out.println("initBinder!! : " + binder.getObjectName());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(dateFormat, false));
}
// Paramter의 날짜 정보를 저장함
// http://localhost:8080/sample/ex03?title=test?&dueDate=2021-03-12 ->
// DateTimeFormat이 없는 경우
// http://localhost:8080/sample/ex03?title=test?&dueDate=2021/03/12 ->
// DateTimeFormat이 있는 경우
@GetMapping("/ex03")
public String ex03(TodoDTO todo) {
log.info("todo: " + todo);
return "ex03";
}
// ====================================Model=================================//
// page는 Bean에 저장 되지 않는다.
// http://localhost:8080/sample/ex04?name=aaa&age=10&page=9
// @GetMapping("/ex04")
// public String ex04(SampleDTO dto, int page) {
//
// log.info("dto: " + dto);
// log.info("page: " + page);
//
// return "/sample/ex04";
// }
// 위와 다르게 @ModelAttribute 사용하여 전달 받은 parameter를 다시 넘겨줌.
// - get set이 없는 parameter를 사용할 때 용이함.
// http://localhost:8080/sample/ex04?name=aaa&age=10&page=9
@GetMapping("/ex04")
public String ex04(SampleDTO dto, @ModelAttribute("page") int page) {
log.info("dto: " + dto);
log.info("page: " + page);
return "/sample/ex04";
}
📕 Model이라는 데이터 전달자
JSP의 request.setAttribute()의 역할
public String HomeController(Model model) {
// 서버 데이터 저장
// 단순히 add만 해주면 Bean으로 만들어진다.
model.addAttribute("serverTime", new java.util.Date());
return "home";
}
메서드의 파라미터를 Model 타입으로 선언하게 되면 자동으로 스프링 MVC에서 Model 타입의 객체를 만들어 주기 때문에 개발자는 사용만 하면 된다.
@ModelAttribute 어노테이션
웹으로 부터 전달 된 파라미터중에 Java Beans 규칙에 맞지 않은 기본 자료형의 경우를 Model에 강제로 넣어준다.
@GetMapping("/ex04")
public String ex04(SampleDTO dto, @ModelAttribute("page") int page) {
log.info("dto: " + dto);
// 웹에서 전달 된 'page' 파라미터를 Model에 강제로 넣어줌
log.info("page: " + page);
return "/sample/ex04";
}
📕 Controller 리턴 타입
// ====================================Return타입================================//
// Return Void
// - 해당 URL의 경로를 그대로 jsp 파일의 이름으로 사용할 때 사용
// http://localhost:8080/sample/voidEx05
@GetMapping("/voidEx05")
public void voidEx05() {
log.info("/voidEx05.............");
}
// Return String
// - 상황에 따라 다른 화면을 보여줄 경우 유용함.
// http://localhost:8080/sample/stringEx05?page=1
@GetMapping("/stringEx05")
public String stringEx05(@ModelAttribute("page") int page) {
// 넘어온 parameter에 따라 다른 return값을 줄 수 있다.
log.info("/stringEx05..............");
if (page == 0) {
return "stringEx05/page0";
} else if (page == 1) {
return "stringEx05/page1";
} else {
return "stringEx05/page2";
}
}
// Return VO, DTO
// - 복합적인 데이터가 들어간 리턴 타입의 경우 주로 JSON 데이터를 만들어 내는 용도로 사용
// - *Jackson-datbind 라이브러리 필수
// http://localhost:8080/sample/ex06
@GetMapping("/ex06")
public @ResponseBody SampleDTO ex06() {
log.info("/ex06..............");
SampleDTO dto = new SampleDTO();
dto.setAge(26);
dto.setName("정재호");
return dto;
}
// Return ResponseEntity
@GetMapping("/ex07")
public ResponseEntity<String> ex07() {
log.info("/ex07................");
// {"name": "정재호"}
String msg = "{\"name\": \"정재호\"}";
HttpHeaders header = new HttpHeaders();
header.add("Content-Type", "application/json;charset=UTF-8");
return new ResponseEntity<String>(msg, header, HttpStatus.OK);
}
// ====================================파일업로드================================================//
@GetMapping("/exUpload")
public void exUpload() {
log.info("/exUpload................");
}
@PostMapping("/exUploadPost")
public void exUploadPost(ArrayList<MultipartFile> files) {
files.forEach(file -> {
log.info("---------------------------");
log.info("name: " + file.getOriginalFilename());
log.info("size: " + file.getSize());
});
}
}
반응형