Spring Boot π
μ§λ ν¬μ€ν μμ Controller, Model, View κ°κ°μ μν κ³Ό μμ± λ°©λ²μ μμ보μλ€.
μ΄λ²μ μ΄ μΈκ°μ§λ₯Ό λμμ μ¬μ©νμ¬
μ¬μ©μμκ² μ λ ₯ νμ΄μ§μμ λ μ μκ°μ λ°μ, μ μ‘ λ²νΌμ λλ₯΄λ©΄ λ κ°μ λν κ°μ μΆλ ₯νμ΄μ§μμ 보μ¬μ£Όλ κΈ°λ₯μ ꡬνν΄λ³΄λλ‘ νλ€.
Controller
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
@GetMapping("/input")
public String input() {
return "/test/input.html";
}
@GetMapping("/result")
public String result(Model model, int num1, int num2) {
model.addAttribute("result", num1 + num2);
return "/test/result.html";
}
}
μ¬μ©μκ° μ λ ₯νλ νμ΄μ§λ₯Ό input λ©μλλ‘,
κ²°κ³Όλ₯Ό 보μ¬μ£Όλ νμ΄μ§λ₯Ό result λ©μλλ‘ λ§λ€μ΄μ£Όκ³ ,
result λ©μλμμ μ¬μ©μκ° μ λ ₯ν int νμ μ num1κ³Ό num2λ₯Ό λ§€κ°λ³μλ‘ λ°λλ€.
(λ§€κ°λ³μλ‘ λ°λ λ°©λ²μ λ§μ§λ§, μ΄μ λν μμΈν μ€λͺ μ λ€μμ λ°λ‘ νκΈ°λ‘ νλ€.)
κ·Έλ¦¬κ³ num1κ³Ό num2μ λν κ°μ resultλΌλ μ΄λ¦(key)μΌλ‘ λͺ¨λΈμ λ΄μμ ν΄λΌμ΄μΈνΈμκ² /test/result.htmlλ₯Ό λ°ννλ€.
input.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>input</title>
</head>
<body>
<form method="get" action="/test/result">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="submit" value="μ μ‘">
</form>
</body>
</html>
result.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>result</title>
</head>
<body>
<h1 th:text="${result}"></h1>
</body>
</html>
input νμ΄μ§μμ μ μ‘ λ²νΌμ λλ₯΄λ©΄ νΈμΆλλ result κ²°κ³Ό νμ΄μ§μλ
νμ리ν λ¬Έλ²μ μ¬μ©ν΄μ result κ°μ νλ©΄μ λμ΄λ€.
νλ‘μ νΈλ₯Ό μ€νν΄λ³΄μ.
νλ‘μ νΈ μ€ν ν,
μ£Όμμ°½μ input νμ΄μ§λ₯Ό μμ²ν΄μ
input νμ΄μ§μ κ°μ μ λ ₯νκ³ μ μ‘λ²νΌμ λλ₯΄λ©΄
λ€μκ³Ό κ°μ΄ result νμ΄μ§μ 3μ΄λΌλ κ°μ΄ λμ¨λ€.
μλ κ·Έλ¦ΌμΌλ‘ μ΄ κ³Όμ μ μ 리ν΄λ³΄μ.