[ 정적 콘텐츠 ]
/static/ 밑에 파일을 만들어서 넣으면 됨
[ MVC와 템플릿 엔진 ]
model, view, controller
view는 그리는데만 집중함, model, controller는 비지니스 로직에 집중
[Web Html 방식]
1. controller
@Controller
public class TestSpring {
@GetMapping("hello")
public String testOut(Model model) {
model.addAttribute("data", "안녕하세요..~~");
return "test"; // templates 밑에 있는 test.html 파일을 찾음
}
@GetMapping("hello-mvc") // 입력받은 url
// 브라우저에서 입력 받는 변수와 값
public String hello(@RequestParam("brouserInput") String thisname, Model model) {
model.addAttribute("thymeleafInput", thisname); // html에서 사용할 변수와 값
return "hello-template"; // templates 밑에 있는 html 파일 이름
}
2. view (templates 밑에있는 파일 "hello-template.html")
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<p th:text="'안녕하세요 : ' + ${thymeleafInput}"> 의미없는 구간이나 나오기는 함</p>
</html>
[API]
api 방식으로 json 형태로 사용자 요청을 전달(단순 텍스트 뿐만 아니라 객체도 전달 가능)
@ResponseBody 가 있어야함.
@GetMapping("spring-api")
@ResponseBody
public String springApi(@RequestParam("brouserInput") String apiStr) {
return "this is input from user brouser : " + apiStr;
}
//2개 이상의 값을 객체로 부터 받을 경우
//(숫자의 경우 int는 안되고 Integer는 됨, 받을 때 객체 형태로 되어 있어야하는 것 같음)
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name, @RequestParam("age") Integer age) {
return new Hello(name, age);
}
static class Hello{
private String name;
private Integer age;
public Hello(String name, Integer age) {
this.name = name; this.age = age;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}
brouser -> /hello-api -> tomcat -> spring container -> search controller -> @ResponseBody
(CALL HttpMessageConverter : jsonConvertor(Object) OR StringConverter(String)) -> send json ({'name': string})
string : StringHttpMessageConverter
object : MappingJackson2HttpMessageConverter
'Spring > spring_old' 카테고리의 다른 글
05./ Spring excersise-Member-01-domain and repository (0) | 2023.01.11 |
---|---|
05./ Spring excersise-Member-설계 (0) | 2023.01.11 |
03./ Spring View 출력 (0) | 2023.01.11 |
02./ spring 환경 구성 검증 (0) | 2023.01.11 |
01./ start.string.io (0) | 2023.01.11 |