[210530] MVC & Template Engine


MVCとTemplate Engine

MVC : Model, View, Controler
以前のJSPはViewですべてを実現した(モデル1)
最近は役の配分が重視されるようになりました.
したがって、Viewでは、画面上の表示部分を考慮する必要があります.
コントローラ、モデル->ビジネスロジックまたは内部処理
まず論理を見てみましょう

Staticと異なり、HTML(変換後)は렌더링です.
#controller/HelloController
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("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model){
        model.addAttribute("name", name);
        return "hello-template";
        }
}
#resources/template/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
パラメータ受信時@RequestParam()
次にlocalhost:8080/hello-mvcに実行して接続します.
エラーが発生します.
2021-05-30 18:16:25.470 WARN 16576 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]
これは@RequestParam()部で発生しました
requiredという名前のオプションがあり、デフォルトはtrueです.
->一言で入れる必要があり、required=falseは入れなくてもいい

デフォルト値はtrueなので、値を超えなければなりません
->localhost:8080/hello-mvc?name=spring!!
*パラメータ情報:制御+P
整理すると.
MVC&templateメソッド
templateエンジンをMVCに分割し、View部分のhtmlをさらにプログラミングし、レンダリング後のhtmlをクライアント(クライアント)に転送します.