package com.example.ex02.controller;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.ex02.domain.vo.InfoDTO;
import lombok.extern.log4j.Log4j;
@Controller
@RequestMapping("/ex/*")
@Log4j
public class SampleController {
@RequestMapping(value = "/basic", method = {RequestMethod.GET, RequestMethod.POST})
public void basic(HttpServletRequest req) {
log.info("basic......"+req.getMethod());
}
@RequestMapping
public void basic2() {
log.info("basic2...............");
}
@GetMapping("/basicOnlyGet")
public void basic3() {
log.info("basic3..........only get");
}
@GetMapping("/ex04")
// 만약 매개변수가 객체라면, 해당 클래스타입의 앞글자만 소문자로 변경된 값이
// 화면에서 사용할 key 값이다.
// 예) InfoDTO infoDTO가 매개변수라면, 화면에서 사용 시 key값은 infoDTO가 된다.
// 만약에 key값을 수정하거나 매개변수가 많아진다면,
// 직접 requestScope에 담아서 전달해야 한다.
// 이 때, request객체를 직접 불러오지 않고, Model이라는 데이터 전달자를 사용하게 된다.
// 하지만 화면 쪽에 전달할 데이터가 여러 개가 아니라면, @ModelAttribute()를 사용하여 화면에 전달해준다.
// @ModelAttribute("화면에서 사용할 KEY")
public String ex04(@ModelAttribute("dto") InfoDTO infoDTO) {
log.info("---------------------------");
log.info("ex04..................");
log.info(infoDTO.toString());
log.info("---------------------------");
return "ex04";
}
@GetMapping("/ex05")
public void ex05(InfoDTO infoDTO, @ModelAttribute("gender") String gender) {
log.info("ex05....................");
log.info(infoDTO.toString());
log.info("gender: "+ gender);
}
@GetMapping("/ex06")
// Model객체는 파라미터로 request객체를 받는다.
// 따라서 여러 개의 데이터를 화면에 전달할 때
// addAttribute(KEY, VALUE)을 사용한다.
// 화면에서는 model에 설정한 KEY로 VALUE를 사용할 수 있다.
public String ex06(InfoDTO infoDTO, String gender, Model model) {
log.info("ex06....................");
log.info(infoDTO.toString());
log.info("gender: " + gender);
model.addAttribute("dto", infoDTO);
model.addAttribute("gender", gender);
return "ex/ex06";
}
}
JSP 구현
ex05
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EX05</title>
<style>
table{
text-align: center;
}
</style>
</head>
<body>
<h1>EX05</h1>
<table border="1">
<tr>
<th>이름</th>
<th>나이</th>
<th>성별</th>
</tr>
<tr>
<td><c:out value="${infoDTO.name}"/></td>
<td><c:out value="${infoDTO.age}"/></td>
<td><c:out value="${gender}"/></td>
</tr>
</table>
</body>
</html>
ex06
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EX06</title>
<style>
table{
text-align: center;
}
</style>
</head>
<body>
<h1>EX06</h1>
<table border="1">
<tr>
<th>이름</th>
<th>나이</th>
<th>성별</th>
</tr>
<tr>
<td><c:out value="${dto.name}"/></td>
<td><c:out value="${dto.age}"/></td>
<td><c:out value="${gender}"/></td>
</tr>
</table>
</body>
</html>