실습
- [Web_Spring] 05 이어서
1. src/main/resource/ex05.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>EX05</title>
</head>
<body>
<h1>EX05</h1>
<table border="1">
<tr>
<th>이름</th>
<th>국어</th>
<th>영어</th>
<th>수학</th>
<th>총점</th>
<th>평균</th>
</tr>
<tr th:object="${vo}">
<td th:text="*{name}"></td>
<td th:text="*{kor}"></td>
<td th:text="*{eng}"></td>
<td th:text="*{math}"></td>
<td th:text="*{total} + '점'"></td>
<td th:text="|*{avg}점|"></td>
</tr>
</table>
</body>
</html>
2. src/main/resource/templates.ex/ex06.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>EX06</title>
</head>
<body>
<h1>EX06</h1>
<table border="1">
<tr>
<th>이름</th>
<th>나이</th>
<th>성별</th>
<th>키</th>
</tr>
<tr>
<td th:text="${exampleVO.name}"></td>
<td th:text="${exampleVO.age}"></td>
<td th:text="${exampleVO.gender}"></td>
<td th:text="|${height}cm|"></td>
</tr>
</table>
</body>
</html>
3. src/main/resource/templates.ex/ex07.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>EX07</title>
</head>
<body>
<h1>EX07</h1>
<table border="1">
<tr>
<th>이름</th>
<th>나이</th>
<th>성별</th>
<th>키</th>
<th>몸무게</th>
</tr>
<tr>
<td th:text="${exampleVO.name}"></td>
<td th:text="${exampleVO.age}"></td>
<td th:text="${exampleVO.gender}"></td>
<td th:text="|${height}cm|"></td>
<td th:text="|${weight}kg|"></td>
</tr>
</table>
</body>
</html>
4. src/main/java/com.example.ex02/domain.vo/StudentVO.java
package com.example.ex02.domain.vo;
import lombok.Data;
import org.springframework.stereotype.Component;
@Component
@Data
public class StudentVO {
private String name;
private Integer kor;
private Integer eng;
private Integer math;
private Integer total;
private Double avg;
}
5. src/main/java/com.example.ex02/controller/ExampleController.java
package com.example.ex02.controller;
import com.example.ex02.domain.vo.ExampleVO;
import com.example.ex02.domain.vo.StudentVO;
import com.example.ex02.domain.vo.TestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/ex/*")
@Slf4j
public class ExampleController {
@RequestMapping(value = "example", method = {RequestMethod.POST, RequestMethod.GET})
public void ex01(){
log.info("ex01...................");
}
@GetMapping("ex02")
public void ex02(){
log.info("ex02.................");
}
@GetMapping("ex03")
public void ex03(ExampleVO exampleVO){
log.info("---------------------------------");
log.info(exampleVO.toString());
log.info("---------------------------------");
}
// 이름, 국어, 영어, 수학 점수를 전달받은 뒤 각 요소를 화면에 출력한다.
@GetMapping("ex04")
public void ex04(TestVO testVO){
log.info("--------------------------------------");
log.info(testVO.toString());
log.info("--------------------------------------");
}
// 숙제
// 화면에서 총점과 평균을 구하지 않고 컨트롤러에서 구한 뒤 화면에 결과를 전달한다.
@GetMapping("ex05")
public String ex05(@ModelAttribute("vo") StudentVO studentVO/*, Model model*/){
// Model
// 화면으로 데이터를 전달할 수 있는 객체
// 2개 이상의 데이터를 전달할 때 적합하고, 한 개의 데이터를 전달할 때에는
// @ModelAttribute("KEY")를 사용하여 간단하게 데이터를 전달할 수 있다.
log.info("--------------------------------------");
log.info(studentVO.toString());
log.info("--------------------------------------");
int total = studentVO.getKor() + studentVO.getEng() + studentVO.getMath();
double avg = total / 3.0;
studentVO.setTotal(total);
studentVO.setAvg(Double.parseDouble(String.format("%.2f", avg)));
// model.addAttribute("vo", studentVO);
return "ex05";
}
@GetMapping("ex06")
public void ex06(ExampleVO exampleVO, @ModelAttribute("height") Double height){
log.info("--------------------------------------");
log.info(exampleVO.toString());
log.info(String.valueOf(height));
log.info("--------------------------------------");
}
// ExampleVO에 있는 이름과 나이를 전달받고, 키와 몸무게 전부 화면에 출력
@GetMapping("ex07")
// @RequestParam : 전달받는 파라미터의 이름이 매개변수의 이름과 다를 때 직접 설정하는 방법
public void ex07(ExampleVO exampleVO, Double height, @RequestParam("data") Double weight, Model model){
log.info("--------------------------------------");
log.info(exampleVO.toString());
log.info(String.valueOf(height));
log.info(String.valueOf(weight));
log.info("--------------------------------------");
model.addAttribute("height", height);
model.addAttribute("weight", weight);
}
}
'웹 개발 > Spring' 카테고리의 다른 글
[Web_Spring] 08 (0) | 2022.06.20 |
---|---|
[Web_Spring] 07 (0) | 2022.06.19 |
[Web_Spring] 05 (0) | 2022.06.17 |
[Web_Spring] 03 (0) | 2022.06.15 |
[Web_Spring] 02 (0) | 2022.06.14 |