[스프링 예제] 2. 스프링 웹 개발 기초
정적 컨텐츠
-
서버에서 하는 것 없이 파일을 그대로 웹브라우저에 다운.
-
-
간단 예)
-
\resources\static\hello-spring.html
<!DOCTYPE HTML>
<html>
<head>
<title>static content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
정적 컨텐츠 입니다.
</body>
</html>
-
결과
-
동작
MVC와 템플릿 엔진
-
MVC : Model, View, Controller
- Model-1을 사용하지 않고 MVC를 사용하는 이유 : 분업
-
Controller
, Model
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping
public String helloMvc(@RequestParam("name") String name, Model model){
model.addAttribute("name",name);
return "hello-template";
}
}
-
View
-
resources\templates\hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
-
결과 (⇒ @RequestParam("name") String name
을 ?
, =
를 통해서 넘긴다.
-
동작
API
-
MVC와 템플릿 엔진 : html
정보를 넘긴다.
-
API : 데이터
정보를 넘긴다.
-
예제 1)
-
Controller
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name){
return "hello" + name; //html 문을 return에 직접 기입할 수도 있다.
}
}
@ResponseBody
: html
에 존재하는 <body>
부에 ❤직접 데이터를 넣겠다.
-
결과
-
예제 2)
-
Controller
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("hello-api")
@ResponseBody
public Hello hello(@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;
}
//getter, setter : property 방식
}
}
-
결과 (⇒ Json
의 결과가 나타난다. {}
: 객체)
-
동작
@ResponseBody
를 사용
- HTTP의 BODY에 문자 내용을 직접 반환
viewResolver
대신에 HttpMessageConverter
가 동작
- 기본 문자처리:
StringHttpMessageConverter
- 기본 객체처리:
MappingJackson2HttpMessageConverter
- byte 처리 등등 기타 여러
HttpMessageConverter
가 기본으로 등록되어 있음
Author And Source
이 문제에 관하여([스프링 예제] 2. 스프링 웹 개발 기초), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@jinmin2216/스프링-예제-2.-스프링-웹-개발-기초
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
서버에서 하는 것 없이 파일을 그대로 웹브라우저에 다운.
간단 예)
-
\resources\static\hello-spring.html
<!DOCTYPE HTML> <html> <head> <title>static content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> 정적 컨텐츠 입니다. </body> </html>
-
결과
-
동작
-
MVC : Model, View, Controller
- Model-1을 사용하지 않고 MVC를 사용하는 이유 : 분업
-
Controller
,Model
package hello.hellospring.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @GetMapping public String helloMvc(@RequestParam("name") String name, Model model){ model.addAttribute("name",name); return "hello-template"; } }
-
View
-
resources\templates\hello-template.html
<html xmlns:th="http://www.thymeleaf.org"> <body> <p th:text="'hello ' + ${name}">hello! empty</p> </body> </html>
-
-
결과 (⇒
@RequestParam("name") String name
을?
,=
를 통해서 넘긴다. -
동작
API
-
MVC와 템플릿 엔진 : html
정보를 넘긴다.
-
API : 데이터
정보를 넘긴다.
-
예제 1)
-
Controller
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name){
return "hello" + name; //html 문을 return에 직접 기입할 수도 있다.
}
}
@ResponseBody
: html
에 존재하는 <body>
부에 ❤직접 데이터를 넣겠다.
-
결과
-
예제 2)
-
Controller
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@GetMapping("hello-api")
@ResponseBody
public Hello hello(@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;
}
//getter, setter : property 방식
}
}
-
결과 (⇒ Json
의 결과가 나타난다. {}
: 객체)
-
동작
@ResponseBody
를 사용
- HTTP의 BODY에 문자 내용을 직접 반환
viewResolver
대신에 HttpMessageConverter
가 동작
- 기본 문자처리:
StringHttpMessageConverter
- 기본 객체처리:
MappingJackson2HttpMessageConverter
- byte 처리 등등 기타 여러
HttpMessageConverter
가 기본으로 등록되어 있음
Author And Source
이 문제에 관하여([스프링 예제] 2. 스프링 웹 개발 기초), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@jinmin2216/스프링-예제-2.-스프링-웹-개발-기초
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
MVC와 템플릿 엔진 : html
정보를 넘긴다.
API : 데이터
정보를 넘긴다.
예제 1)
-
Controller
package hello.hellospring.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @GetMapping("hello-string") @ResponseBody public String helloString(@RequestParam("name") String name){ return "hello" + name; //html 문을 return에 직접 기입할 수도 있다. } }
@ResponseBody
:html
에 존재하는<body>
부에 ❤직접 데이터를 넣겠다.
-
결과
예제 2)
-
Controller
package hello.hellospring.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @GetMapping("hello-api") @ResponseBody public Hello hello(@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; } //getter, setter : property 방식 } }
-
결과 (⇒
Json
의 결과가 나타난다.{}
: 객체) -
동작
@ResponseBody
를 사용- HTTP의 BODY에 문자 내용을 직접 반환
viewResolver
대신에HttpMessageConverter
가 동작- 기본 문자처리:
StringHttpMessageConverter
- 기본 객체처리:
MappingJackson2HttpMessageConverter
- byte 처리 등등 기타 여러
HttpMessageConverter
가 기본으로 등록되어 있음
Author And Source
이 문제에 관하여([스프링 예제] 2. 스프링 웹 개발 기초), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jinmin2216/스프링-예제-2.-스프링-웹-개발-기초저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)