반응형
정적컨텐츠 : 파일 그대로 고객에게 전달(프로그래밍 불가)
static 폴더 안에 html을 만들고
localhost:8080/파일이름.html 하면 html이 나옴
MVC와 템플릿엔진 : 서버에서 변경을 해서 HTML 로 내려줌
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam(value="name") String name, Model model){
model.addAttribute("name",name);
return "hello-template";
}
hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
localhost:8080/hello-mvc?name="spring"
파라미터를 입력할 때는 ?를 이용
name을 지정해주지 않으며 에러남
@ResponseBody를 사용하면 html없이 바로 문자만 내려감
@GetMapping("hello-spring")
@ResponseBody
public String helloSpring(@RequestParam("name") String name){
return "hello " + name;
}
API : 객체를 반환하는 것 (객체가 오면 Json Converter -> String Converter)
자바 문법으로 class 안에 class 또 만들 수 있음
Hello 객체 안에 입력 받은 name을 넣어 객체를 반환하는 것
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name){
Hello hello = new Hello();
hello.setName(name);
return hello;
}
static class Hello{
private String name;
public String getName() {
return name;
}
public void setName(String name){
this.name = name;
}
}
localhost:8080/hello-api?name=spring
이렇게 json 형태로 나온다.
나는 chrome 웹 스토어에서 json view를 검색했을 때 별 5개짜리 확장 프로그램을 다운 받아놓았기에 이렇게 보인다.
반응형
'🍀spring > 스프링 입문' 카테고리의 다른 글
[View 환경설정] Spring 윈도우 터미널 빌드(자바 버전 맞지 않을 때) (0) | 2022.03.18 |
---|---|
[스프링 부트] 테스트 라이브러리, 스프링 부트 라이브러리 (0) | 2022.03.18 |
[프로젝트 환경 설정] 스프링 입문 - 코드로 배우는 스프링 부트 (0) | 2022.03.17 |