
Controller๋ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ๋ผ ์๋ฒ์์ ์ด๋ฅผ ์ฒ๋ฆฌํ๋ ์ญํ ์ ํฉ๋๋ค.Model์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ์ญํ ์ ํฉ๋๋ค.View๋ ํ๋ฉด์ ๋ด๋นํ๋ ๋ทฐ ํ
๋ธ๋ฆฟ์
๋๋ค.์นํ์ด์ง๋ฅผ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ณ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ์ ์ฒ๋ฆฌํ๊ณ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ์ญํ๋ฅผ ๋๋๋ ๊ธฐ๋ฒ์ด MVC ํจํด์ด๋ผ๊ณ ํฉ๋๋ค.
๐ฆ MVC(Model-View-Controller) ํจํด์ ์๋ ๊ณผ์
ํด๋ผ์ด์ธํธ ์์ฒญ์ ๋ฐ์ ์ฒ๋ฆฌํ์ฌ view๋ก ์๋ตํ๋ ๊ณผ์ ์ ๋๋ค.
Controller:
Model:
View:
์ด๋ฌํ ๊ณผ์ ์ ํตํด ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ์๋ค์ฌ ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ฃผ๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌํํ ์ ์์ต๋๋ค. ์ด ๊ณผ์ ์์ Controller๋ ์์ฒญ์ ๋ฐ์ ์ฒ๋ฆฌํ๊ณ , View๋ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์ญํ ์ ์ํํ๋ฉฐ, Model์ Controller์ View ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ์ญํ ์ ํฉ๋๋ค.
view๋ฅผ ๋ฐํํ๋ ๋ฐฉ๋ฒ
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
@Controller
@RequestMapping("main7")
public class Controller07 {
// /main7/sub5
// ์์ฒญ ๊ฒฝ๋ก์ view ์ด๋ฆ์ด ๊ฐ์ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค
@RequestMapping("sub5")
public String method5() {
return "main7/sub5";
}
@RequestMapping("sub6")
public String method6() {
// ์์ฒญ ๊ฒฝ๋ก์ ๊ฐ์ view ์ด๋ฆ์ผ๋ก ๊ฒฐ์ ๋จ
return null;
}
@RequestMapping("sub7")
public void method7() {
// ์์ฒญ ๊ฒฝ๋ก์ ๊ฐ์ view์ ์ด๋ฆ์ผ๋ก ๊ฒฐ์ ๋์ด
// ๋ฆฌํด์ ์ํด๋ ๋จ
// view : /WEB-INF/view/main7/sub7.jsp
}
}
method5():
method6():
method7():
์์ฝํ๋ฉด, ์ปจํธ๋กค๋ฌ ๋ฉ์๋๋ ๋ค์ํ ๋ฐฉ์์ผ๋ก ๋ทฐ๋ฅผ ๋ฐํํ ์ ์์ต๋๋ค. ์ด๋ Spring MVC๊ฐ ์ ์ฐํ๊ฒ ๋ทฐ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋๋ก ํฉ๋๋ค. ์์ฒญ ๊ฒฝ๋ก์ ๋ทฐ ์ด๋ฆ ์ฌ์ด์ ๋งคํ์ ViewResolver์ ์ํด ๊ด๋ฆฌ๋๋ฉฐ, ์ปจํธ๋กค๋ฌ์ ๋ฐํ ๊ฐ์ ๋ฐ๋ผ ์ด ๋งคํ์ด ์ฒ๋ฆฌ๋ฉ๋๋ค.
String
@RequestMapping("sub2")
public void method2(Model model) {
model.addAttribute("city", "์์ธ");
model.addAttribute("address", "์ ์ด");
}
<body>
<h1> ๋์๋ '${city}' ์
๋๋ค.</h1>
<h1> ์ฃผ์๋ '${address}' ์
๋๋ค.</h1>
</body>
๋ฐฐ์ด
@RequestMapping("sub5")
public void method5(Model model) {
model.addAttribute("cities", new String[]{"์์ธ", "๋๊ตฌ", "๋ถ์ฐ"});
model.addAttribute("countries", new String[]{"ํ๊ตญ", "๋ฏธ๊ตญ", "์ผ๋"});
}
<body>
<h1>${cities[0]}, ${cities[1]}, ${cities[2]}</h1>
<h1>${countries[0]}, ${countries[1]}, ${countries[2]}</h1>
</body>
List
@RequestMapping("sub6")
public void method6(Model model) {
List<String> data = List.of("java", "css", "html", "jsp");
model.addAttribute("myList", data);
}
<body>
<h1>${myList}</h1>
<h1>${myList[0]}</h1>
<h1>${myList[1]}</h1>
<h1>${myList[2]}</h1>
<h1>${myList[3]}</h1>
</body>
Map
@RequestMapping("sub9")
public void method9(Model model) {
Map<String, String> map = new HashMap<>();
map.put("name", "ํฅ๋ฏผ");
map.put("age", "์๋ฅธ");
map.put("address", "๋ฐ๋");
model.addAttribute("myMap", map);
}
<body>
<h1>${myMap["name"]}</h1>
<h1>${myMap["age"]}</h1>
<h1>${myMap["address"]}</h1>
<hr>
<%-- attributeName.key --%>
<h1>${myMap.name}</h1>
<h1>${myMap.age}</h1>
<h1>${myMap.address}</h1>
</body>
myMap["ํค"]๋ก ์์ฑํด์ผ ํฉ๋๋ค.JavaBeans
JavaBeans๋ ์๋ฐ ์ธ์ด์ ์ผ๋ฐ์ ์ธ ํ๋ก๊ทธ๋๋ฐ ๊ด๋ก์ ๋ฐ๋ผ ์์ฑ๋ ์๋ฐ ํด๋์ค(์๋ฐ ๊ฐ์ฒด)์
๋๋ค.
public class MyBean094 {
private String modelNumber;
private Integer productPrice;
private String name;
private Double weight;
private boolean used;
public String getModelNumber() {
return modelNumber;
}
public void setModelNumber(String modelNumber) {
this.modelNumber = modelNumber;
}
public Integer getProductPrice() {
return productPrice;
}
public void setProductPrice(Integer productPrice) {
this.productPrice = productPrice;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
public boolean isUsed() {
return used;
}
public void setUsed(boolean used) {
this.used = used;
}
}
@RequestMapping("sub4")
public void sub4(Model model) {
MyBean094 myBean094 = new MyBean094();
myBean094.setModelNumber("k5");
myBean094.setProductPrice(5000);
myBean094.setName("kia");
myBean094.setWeight(2400.50);
myBean094.setUsed(true);
model.addAttribute("car", myBean094);
}
<body>
<h1>${car.modelNumber}</h1>
<h1>${car.productPrice}</h1>
<h1>${car.name}</h1>
<h1>${car.weight}</h1>
<h1>${car.used}</h1>
</body>