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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

HTML

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

Spring Boot

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

Q&A

解決済

2回答

4344閲覧

Spring boot 入門 ControllerとHTMLの連携

退会済みユーザー

退会済みユーザー

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

HTML

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

Spring Boot

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

0グッド

0クリップ

投稿2019/11/17 11:48

編集2019/11/17 11:58

前提・実現したいこと

Spring bootを使いcontrollerからHTMLを呼びブラウザ上に表示できるようにする。

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

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Nov 17 20:30:36 JST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

該当のソースコード

Java
Controllerクラス
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Democontroller {
@RequestMapping(value="/", method=RequestMethod.GET)
public ModelAndView index(ModelAndView mav){
mav.setViewName("index2");
mav.addObject("msg", "メッセージ");
return mav;
}
}
HTML

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index2</title> </head> <body> <p class="msg" th:text="${msg}"></p> </body> </html>

Spring bootアプリケーション実行でのコンソール
Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

. ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
/ __)| |)| | | | | || (
| | ) ) ) )
' || .__|| ||| |_, | / / / /
=========|
|==============|
/=////
:: Spring Boot :: (v2.2.1.RELEASE)

2019-11-17 20:41:52.920 INFO 11688 --- [ restartedMain] com.example.demo.Application : Starting Application on DESKTOP-G9PNTDK with PID 11688 (C:\Users\famil\eclipse-workspace\Spring\target\classes started by famil in C:\Users\famil\eclipse-workspace\Spring)
2019-11-17 20:41:52.923 INFO 11688 --- [ restartedMain] com.example.demo.Application : No active profile set, falling back to default profiles: default
2019-11-17 20:41:52.973 INFO 11688 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-11-17 20:41:52.974 INFO 11688 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-11-17 20:41:54.115 INFO 11688 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-17 20:41:54.125 INFO 11688 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-17 20:41:54.125 INFO 11688 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-17 20:41:54.239 INFO 11688 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-17 20:41:54.239 INFO 11688 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1265 ms
2019-11-17 20:41:54.509 INFO 11688 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-17 20:41:54.708 INFO 11688 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-11-17 20:41:55.071 INFO 11688 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-17 20:41:55.071 INFO 11688 --- [ restartedMain] com.example.demo.Application : Started Application in 2.547 seconds (JVM running for 3.725)

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.2.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring</name>
<description>Demo project for Spring Boot</description>

<properties> <java.version>13</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </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>

試したこと

Chromeでhttp://localhost:8080/を入力

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

A-pZ

2019/11/17 11:53

念のため、動作させようとしているspring-bootのpom.xmlも質問に追記してくださいますか。
guest

回答2

0

ベストアンサー

Spring Boot は @SpringBootApplication が付与されたクラスが配置されているパッケージを起点として、@Controller クラスなどを探索するコンポーネントスキャンを行います。
今回のケースでは、com.example.demo.Application がそのクラスとなりますので、起点となるパッケージは com.example.demo となります。

そのため、Controller を配置するパッケージを com.example.democom.example.demo.controller など起点となるパッケージを含むように修正する必要があります。

投稿2019/11/17 23:35

d-yosh

総合スコア270

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

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

退会済みユーザー

退会済みユーザー

2019/11/18 12:45

Controllerを入れるパッケージを変更したらできるようになりました。 ありがとうございます。
guest

0

setViewNameにて、index2 を指定しているので、index2.html を、
src/main/resorces/template/ の中に配置すれば動作します。

@RequestMappingの記述がやや古いのがあるのと、Controllerのクラス名がやや気になる以外は特に問題はありませんでした。

投稿2019/11/17 14:05

A-pZ

総合スコア12011

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

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

退会済みユーザー

退会済みユーザー

2019/11/17 14:19

ご回答ありがとうございます。htmlファイルは既にsrc/main/resorces/template/の中に配置している状態でございます。 @RequestMappingはどのように変えるのがよろしいでしょうか。 8080でアクセスしてもeclipse上のコンソールには何も表示されませんでした。
退会済みユーザー

退会済みユーザー

2019/11/17 14:34

controllerは Spring/src/main/java/controller(パッケージ)/ の中にあります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問