SpringBoot在不使用模板的情况下配置html静态资源

1.目录结构

8EC04250-2663-4784-8A1A-6A3F227A5EBC.png


2.pom.xml引入静态资源

<build>

    <plugins>

        <plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

    </plugins>

    <resources>

        <resource>

            <directory>src/main/resources</directory>

            <includes>

                <include>**/*.xml</include>

                <include>**/*.properties</include>

                <include>**/*.html</include>

                <include>**/*.css</include>

                <include>**/*.js</include>

            </includes>

            <filtering>true</filtering>

        </resource>

    </resources>

</build>


3.添加视图配置

# view

spring.mvc.static-path-pattern=/static/**

spring.resources.static-locations=classpath:/static, classpath:/templates

spring.mvc.view.suffix=.html


静态资源统一以 /static 开头;

静态资源在 static templates 目录中;

视图以 .html 结尾;


4.在Controller中添加RequestMapping

@RequestMapping("/index")

public String index() {

    return "index";

}


5.浏览器访问

http://127.0.0.1:8080/static/index.html



 

展开阅读全文