LoginSignup
0
0

More than 3 years have passed since last update.

「先生ー、Springでログイン機能を実装したいでーす」①HelloWorld

Posted at

 導入

1,HelloWorld
2,ログインフォーム
3,データ保存
4,ログイン

ダウンロード

STSのダウンロード
迷ったら
WindowsにSpring Tool Suiteを導入する -Qiita

プロジェクトの作成

1,File⇒New⇒Spring Starter Project
1.jpg
1,「Name」=「HelloWorld」
2,「Package」=「com.login」
2.jpg
1,Web⇒Spring Web
3.jpg

クラス作成

1,「com.login」上で、右クリック。New⇒Class
4.jpg
1、「Name」=「HomeController」
5.jpg

1,作成したクラスに下記を上書き

HomeController
package com.login;

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

@Controller
public class HomeController {
    @GetMapping("/hello")
    public String home(Model model) {
        model.addAttribute("hello","Hello World!");
        return "hello";
    }
}

1,pom.xmlに下記を追加

pom.xml
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

10.jpg

HTMLファイルを作成

1、src/main/resourcesの
「templates」上で右クリック。New⇒Other
6.jpg
1、「html」入力。
2、「HTMLFile」選択。
7.jpg
1、「File name」=「hello.html」
8.jpg

1、作成した「hello.html」に下記を上書き。

hello.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1 th:text="${hello}"></h1>
</body>
</html>

実行

プロジェクト「HelloWorld」上で右クリック。「Run As」⇒「Spring Boot App」
9.jpg
1,ブラウザ上で入力⇒「localhost:8080/hello」
WS000003.JPG

所感

先生:(結果はhtml拡張子のファイルに「HelloWorld」を書いたものと変わらんが、サーバーサイドをかますことで、可能性の素晴らしさに気づくだろう)
たかし:先生ー、これってhtml拡張子のファイルに「HelloWorld」書いたのと同じじゃね?
先生:・・・
サーバーをかますことで、300億でも何でも、1から順番に数を表示してくれるし、
人間の脳みそじゃ理解に時間がかかる暗号を作ったり、読み解いたりできる。
しかも、3の倍数と3がつく数字の時に「AHO」になるための練習文だって表示できるんだぞ!

CreateAho

@GetMapping("/hello")
    public String home(Model model) {
        List<String> aho=new ArrayList<String>();
        int a,b,c;
        for(int i=1;i<=1000;i++) {
            a=i/10;
            b=i/100;
            c=i/1000;
            if(i%3==0 || i%10==3 || a%10==3|| b%10==3||c%10==3) {
                aho.add("AHO");
                continue;
            }
            aho.add(String.valueOf(i));
        }
        model.addAttribute("hello", aho);
        return "hello";
    }

したことの確認

①Javaプロジェクトの作成(Spring)
②Controllerクラスの作成(Mappingを行う)
③Htmlファイルの作成(サーバーサイドの機能を使用)
④プロジェクトの設定ファイルを編集(Mavenのxml)
⑤Webサーバソフトウェアを用いたWebページの表示(apache)

0
0
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
0