質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
IntelliJ IDEA

IntelliJ IDEA(インテリジェイ アイディア)は、JetBrains社が開発した、 JavaやScalaなどで利用される統合開発環境です。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

3249閲覧

index.htmlから次の画面遷移の方法が分かりません。spring boot + Thymeleaf + intelliJ

erikan3b

総合スコア1

IntelliJ IDEA

IntelliJ IDEA(インテリジェイ アイディア)は、JetBrains社が開発した、 JavaやScalaなどで利用される統合開発環境です。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2020/08/19 12:19

前提・実現したいこと

私は以前、簡単なレシピ検索・投稿のwebアプリケーションを
Java・JSP・Servlet・eclipsを使って作りました。

勉強の為、このwebアプリをフレームワークを使って再挑戦しているところです。
Java Spring Boot・Thymeleaf・IntelliJを使ってみているのですが
どれも超初心者です!

発生している問題・エラーメッセージ

index.htmlからhome.htmlに画面遷移しない。

該当のソースコード

Spring Bootの設定
Spring Bootの設定
pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>erika_cookbase</artifactId> <version>0.0.1-SNAPSHOT</version> <name>erika_cookbase</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ```resources/static/index.html ```ここに言語名を入力 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookBase_index</title> </head> <body> <h1>Hello indexだよ~、どうやってhomeに行けるの?</h1> </body> </html>

http://localhost:8080/
イメージ説明
src/java/controller/User

package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import java.awt.*; @Controller public class Login { @GetMapping("/") public String index(){ return "/index"; } @GetMapping("/index") public String home(){ return "/home"; } }

試したこと

15分ではなく、何日もサイトで情報を探したのですが、 ほとんどの記事がformやlinkを使ってリクエストパラメーターを取得していました。

発生している問題・エラーメッセージ

JSPの時のように、アプリを動かしたら、index.htmlからhome.htmlを画面に出したい。 (そもそも必要なことなのかさえも分かりません) http://localhost:8080/homeと指定してもエラーになる。

resources/templates/home.html

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookBase_home</title> </head> <body> <h1>Welcome to Erika cookBase</h1> <form method="post" action="/"> <input type="text" name="name"> <input type="password" name="pass"> <input type="submit" value="login"> <input type="submit" value="sign up"> </form> </body> </html>

見てくださった方へ

こんな質問をしてしまう自分を情けなく思います。
今回勇気を出して初めての質問をさせていただきました。
私にとって第一歩になるかと思います。どなたかお力添えをいただけましたら幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Controller の@XXXMapping で指定した path がリクエストパスになります。
質問に記載された Login だと、以下の対応になっています。

  • localhost:8080/ で index()メソッドが呼ばれる
  • localhost:8080/index でhome()メソッドがよばれる

なので、resources/static/home.html などのファイルを用意しているのであれば、
localhost:8080/index にGETリクエストすると home に遷移できると思います。

投稿2020/08/19 16:25

kiino

総合スコア539

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

erikan3b

2020/08/20 07:34

ご親切にありがとうございます!! どうやら?mainメソッドがjava/com.~/の下になかったのも原因だったようで、 教えていただいた通りlocalhost:8080/index にGETリクエストを試したところ、 home に遷移できました。 優しさに感謝します★
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問