[Spring spring入門]2.Spring Web開発基盤
n/a.ターゲット
1.静的コンテンツ
resources/static/hello-static.htmlの生成
Webブラウザで上のURL->埋め込み式Tomcatサーバが要求を受信->サーバがSpringコンテナに要求を渡す->1)コントローラがないため,2)htmlファイルを検索->htmlファイルをWebに返す
2.MVCとテンプレートエンジン
MVC : Model, View, Controller
Controller
業務ロジックを担当する@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
view
担当画面<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body></html>
実行画面
2.1とは異なり、ViewResolverはコントローラで返されたhtmlを検索し、テンプレートエンジン処理後に変換されたhtmlをWebブラウザに返す.
3. API
@ResponseBody戻り文字:Controller
左輪ピストルは使用しません.@GetMapping("hello-string")
@ResponseBody // http의 body부분에 데이터를 넣어준다.
public String helloStringg(@RequestParam("name") String name) {
return "hello " + name;
}
実行画面
return "hello " + name;
この部分をそのままhtmlに入れる.テンプレートエンジンを通らない.
@ResponseBodyオブジェクトを返します:Controller
@ResponseBodyを使用してオブジェクトに戻ると、オブジェクトはJSONに変換されます.@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello; // 클래스 객체를 전달, 객체는 json 형태로 리턴함
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
実行画面
Reference
この問題について([Spring spring入門]2.Spring Web開発基盤), 我々は、より多くの情報をここで見つけました
https://velog.io/@redcarrot01/Spring-스프링-입문-2.-스프링-웹개발-기초
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
MVC : Model, View, Controller
Controller
業務ロジックを担当する
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
view
担当画面
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body></html>
実行画面
2.1とは異なり、ViewResolverはコントローラで返されたhtmlを検索し、テンプレートエンジン処理後に変換されたhtmlをWebブラウザに返す.
3. API
@ResponseBody戻り文字:Controller
左輪ピストルは使用しません.@GetMapping("hello-string")
@ResponseBody // http의 body부분에 데이터를 넣어준다.
public String helloStringg(@RequestParam("name") String name) {
return "hello " + name;
}
実行画面
return "hello " + name;
この部分をそのままhtmlに入れる.テンプレートエンジンを通らない.
@ResponseBodyオブジェクトを返します:Controller
@ResponseBodyを使用してオブジェクトに戻ると、オブジェクトはJSONに変換されます.@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello; // 클래스 객체를 전달, 객체는 json 형태로 리턴함
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
実行画面
Reference
この問題について([Spring spring入門]2.Spring Web開発基盤), 我々は、より多くの情報をここで見つけました
https://velog.io/@redcarrot01/Spring-스프링-입문-2.-스프링-웹개발-기초
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
@GetMapping("hello-string")
@ResponseBody // http의 body부분에 데이터를 넣어준다.
public String helloStringg(@RequestParam("name") String name) {
return "hello " + name;
}
@GetMapping("hello-api")
@ResponseBody
public Hello helloApi(@RequestParam("name") String name) {
Hello hello = new Hello();
hello.setName(name);
return hello; // 클래스 객체를 전달, 객체는 json 형태로 리턴함
}
static class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Reference
この問題について([Spring spring入門]2.Spring Web開発基盤), 我々は、より多くの情報をここで見つけました https://velog.io/@redcarrot01/Spring-스프링-입문-2.-스프링-웹개발-기초テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol