Spring]Spring環境とView設定


スプリングとは


SpringフレームワークはJavaプラットフォーム向けのオープンソースアプリケーションフレームワークであり、簡単にSpringと呼ぶ.主にダイナミックWebサイトの開発に多くのサービスを提供しています.

開発環境構築準備

  • Java 11
  • IntelliJ
  • プロジェクトの作成


  • https://start.spring.io/
    ->本来spring開発者は開発環境に次々と面倒をかける必要がありますが、springガイドを使用することで開発環境を個別に設定する必要はありません

  • 次の設定を選択し、必要な残りの部分を選択します.

  • Java 11

  • Dependencies -> Spring Web, Thymeleaf
  • 運転確認


    src/java/自分で設定したartifactid/hellospringApplication->run

    こうして浮かび上がれば成功する

    ビューの設定


    Whitelabel Error Pageではなく別の画面に設定するにはresource/indexを使用します.htmlファイルを生成します.
    index.html
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
    Hello Spring
    <a href = "/spring">go localhost:8080/spring</a> <!-- localhost:8080/spring으로 이동-->
    </body>
    </html>
    localhost:8080/hello、helloに移動します.受信htmlのコントローラを作成します.
    Controller.java
    package hello.hellospring.controller;
    
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @org.springframework.stereotype.Controller
    public class Controller {
    
        @GetMapping("spring") // localhost:8080/spring을 받으면 아래 메서드 실행
        public String hello(Model model){
            model.addAttribute("key", "spring!!"); // key = data, value = hello!!
            return "hello"; // hello.html로 이동
        }
    }
    
    
    
    hello.html
    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Hello</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p th:text="'Hello. ' + ${key}" ></p>
    </body>
    </html>
  • html xmlns:th="http://www.thymeleaf.org"->時間軸を使用するタグ
  • th:text=「Hello」+${key}->期間構文
  • 実行画面