LoginSignup
0
1

More than 1 year has passed since last update.

Spring Boot勉強② Hello World

Posted at

はじめに

前回の続き、プロジェクト作成後にThymeleafを用いてHello Worldを表示させるまでを行う

を参考に実施

目次

  1. パッケージ構成
  2. HTML,Controllerの作成
  3. Spring Bootプロジェクトの起動
  4. HTML,Controllerの関係性
  5. 参考文献

パッケージ構成

image.png

HTML,Controllerの作成

①「resources」-「templates」配下にindex.htmlを作成
※「resources」-「templates」フォルダ配下が参照されるのはThymeleafのデフォルト、変更も可

index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Hello</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>任意タイトル</h1>
    <h2><span th:text="${message}"></span></h2>
  </body>
</html>

②com.example.demo.controllerパッケージ配下にHelloWorldController.javaを作成

HelloWorldController.java
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloWorldController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!!");
        return "index";
    }
}

Spring Bootプロジェクトの起動

①プロジェクトを右クリック→「実行」→「Spring boot アプリケーション」を選択してSpring Bootプロジェクトを起動
②ブラウザで「http://localhost:8080/」にアクセス

image.png

HTML,Controllerの関係性

アノテーションを基にRequestに応じてControllerのメソッドを実行し、returnで指定されたページに遷移する
image.png

参考文献

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1