Springboot默認訪問首頁(絕對乾貨,完整)

注意:項目中有加入thymeleaf的引擎模板
以下是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 http://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>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bootcrud</groupId>
    <artifactId>springboot-crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-crud</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <!--layout是佈局功能擴展-->
        <!--佈局功能的支持,2.0以上版本支持thymeleaf3-->
        <!--比如:thymeleaf3,layout2以上版本;thymeleaf2,layout1-->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
    </properties>

    <dependencies>
        <!--thymeleaf模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- jquery-webjars -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在properties節點中,加入了模板引擎後的jar,會在resources文件下出現templates的文件夾,我們可以把我們要訪問的html放在這個文件夾下,templates就會自動幫我們找到templates文件夾下的html。
SpringbootApplication是springboot的主配置類,用main方法來啓動springboot。

config文件:

package com.index.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


//使用WebMvcConfigurerAdapter可以來擴展springmvc的功能
@Configuration
//@EnableWebMvc 不要接管Springmvc
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    //所有的WebMvcConfigurer組件都會一起起作用
    //@Bean 將組件註冊在容器中
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adapter = new WebMvcConfigurerAdapter(){
            public void addViewControllers(ViewControllerRegistry registry){
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
            }
        };
        return adapter;
    }

}

以上代碼就類似於我們以前controller中的:

@RequestMapping({"/","index.html"})
public String index(){
  return "login";
}

這就很好理解了。

而在config中的這兩句話,它們兩句話的作用都是一模一樣的。

registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");

可以試着在剛纔的templates文件夾下加入一個html,運行springboot時,都會訪問到主頁面。

注:

registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");setViewName("login")中,login就是html的名字。
如果主頁是index.html,只需要把在setViewName內的login改成index即可訪問到。

在這裏插入圖片描述
分享到這個裏,我打王者去了。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章