Spring Bootトップページ設定解析(推奨)
7226 ワード
まず、Spring Bootトップページに設置されている3つの方法を説明します。
1.SpringBootデフォルトのトップページの設定
ファイルindex.を作成します。

http://localhost:8080/アクセス、成功表示

ソース分析
まず対応する自動配置類WebMvcAutoConfigrationの中の対応コードを探します。

Static LocationsはWebProptisの内部静的な種類のResourceの属性であり、コンストラクタからその値が
/META-INF
/レスポンス/
/レスポンス/
/static/
//public/
前のコードに戻ります。Static Locationsを取得した後、巡回することによって、明らかに新しい方法が見られます。
ソースからは、別の静的リソースディレクトリの優先度の問題を見ることができます。getWelcome Pageは静的リソースディレクトリを遍歴しています。見つけたら戻りますので、優先度とstatic Locationsの中の順序が相対しています。確認してください。
まず各ディレクトリの下に対応するindx.ファイルを作成します。

http://localhost:8080/アクセス

得られた結論と同じように、優先順位が一番高いのは/META-INF/resoures/で、/META-INF/resource/下のindex.ファイルを削除して再検証します。

検証成功!
2.controllerに「/」のマップパスを追加
新規IndexController.java

3.MVC拡張配置実現
新しいMyMvcConfigrationの配置類を作って、MVCの配置を拡張して、addView Controllerの方法を書き直します。

拡張:優先順位の問題
以前の3つの方法は全部単独で設置しました。今は彼らを結合します。

http://localhost:8080/アクセス

優先度が一番高いのは第二の方法です。そしてindexControllerを削除して、再度検証します。

結論:Controller>MyMvcConfigration>デフォルト方法
ここで、SpringBootトップページの設定解析について詳しく紹介します。SprigBootトップページの設定内容は以前の文章を検索したり、下記の関連記事を見たりしてください。これからも応援してください。
1.SpringBootデフォルトのトップページの設定
ファイルindex.を作成します。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1> </h1>
</body>
</html>
index.ファイルをSpring Bootの任意の静的リソースディレクトリの下に置く。
http://localhost:8080/アクセス、成功表示

ソース分析
まず対応する自動配置類WebMvcAutoConfigrationの中の対応コードを探します。
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping =
new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext),
applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
return welcomePageHandlerMapping;
}
SpringBootはWelcome PageHandlerMappingBenを登録してプロジェクトのデフォルトのトップページを処理します。コンストラクタの中のthis.get Welcome Pageはトップページの資源です。
private Resource getWelcomePage() {
String[] var1 = this.resourceProperties.getStaticLocations();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
String location = var1[var3];
Resource indexHtml = this.getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
ServletContext servletContext = this.getServletContext();
if (servletContext != null) {
return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/")));
} else {
return null;
}
}
このコードを分析して、まずthis.resource PropertiesのStatic Locationsフィールドを獲得しました。名前の通り静的なパスです。まずStatic Locationsを追跡します。
Static LocationsはWebProptisの内部静的な種類のResourceの属性であり、コンストラクタからその値が
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
明らかに、これはSpringBootの静的資源目録です。/META-INF
/レスポンス/
/レスポンス/
/static/
//public/
前のコードに戻ります。Static Locationsを取得した後、巡回することによって、明らかに新しい方法が見られます。
private Resource getIndexHtml(String location) {
return this.getIndexHtml(this.resourceLoader.getResource(location));
}
this.resource Loaderを使用して、locationに対応するResourceに戻り、別のget IndexHtml()関数を実行します。
private Resource getIndexHtml(Resource location) {
try {
Resource resource = location.createRelative("index.html");
if (resource.exists() && resource.getURL() != null) {
return resource;
}
} catch (Exception var3) {
}
return null;
}
明らかに、この方法は対応ディレクトリのindex.ファイルを取得することです。また振り返ってみます
for(int var3 = 0; var3 < var2; ++var3) {
String location = var1[var3];
Resource indexHtml = this.getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
対応するファイルが見つかったら、対応するリソースを返します。これはSpringBoot設定のトップページのデフォルト方式の原理です。ソースからは、別の静的リソースディレクトリの優先度の問題を見ることができます。getWelcome Pageは静的リソースディレクトリを遍歴しています。見つけたら戻りますので、優先度とstatic Locationsの中の順序が相対しています。確認してください。
まず各ディレクトリの下に対応するindx.ファイルを作成します。

http://localhost:8080/アクセス

得られた結論と同じように、優先順位が一番高いのは/META-INF/resoures/で、/META-INF/resource/下のindex.ファイルを削除して再検証します。

検証成功!
2.controllerに「/」のマップパスを追加
新規IndexController.java
package com.springboot04webapp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/")
public String index(){
return "indexController";
}
}
最初のページの資源indexController.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>indexController </h1>
</body>
</html>
http://localhost:8080/アクセス
3.MVC拡張配置実現
新しいMyMvcConfigrationの配置類を作って、MVCの配置を拡張して、addView Controllerの方法を書き直します。
package com.springboot04webapp.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("indexMVC");
}
}
トップページの資源indexMVC.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>indexMVC </h1>
</body>
</html>
http://localhost:8080/アクセス
拡張:優先順位の問題
以前の3つの方法は全部単独で設置しました。今は彼らを結合します。

http://localhost:8080/アクセス

優先度が一番高いのは第二の方法です。そしてindexControllerを削除して、再度検証します。

結論:Controller>MyMvcConfigration>デフォルト方法
ここで、SpringBootトップページの設定解析について詳しく紹介します。SprigBootトップページの設定内容は以前の文章を検索したり、下記の関連記事を見たりしてください。これからも応援してください。