天天看点

springBoot访问静态资源

新建项目

springBoot访问静态资源
springBoot访问静态资源
springBoot访问静态资源
springBoot访问静态资源

我们把一张图片放在static的目录下

springBoot访问静态资源

 编写App.java启动类

springBoot访问静态资源

App.java

package com.gongspringstatic.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBoot 启动类
 * @author Administrator
 *
 */
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}      

 运行启动类

springBoot访问静态资源
springBoot访问静态资源

 在浏览器访问

springBoot访问静态资源

 新建一个index.html

springBoot访问静态资源
springBoot访问静态资源
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>静态资源访问方式一</title>
</head>
<body>
     静态资源访问方式一
</body>
</html>      

启动App.java

springBoot访问静态资源

 打开浏览器访问

springBoot访问静态资源

 新建images包,并把java.jpg放进去

springBoot访问静态资源

在index.html中添加一下语句

springBoot访问静态资源
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>静态资源访问方式一</title>
</head>
<body>
     静态资源访问方式一
     <hr/>
     <img alt="" src="images/java.jpg">
</body>
</html>      

启动App.java类

springBoot访问静态资源
springBoot访问静态资源

 在浏览器中访问

springBoot访问静态资源

访问静态资源的第二种方法

 我们重新创建一个项目

springBoot访问静态资源
springBoot访问静态资源
springBoot访问静态资源
springBoot访问静态资源

 在main目录下创建webapp目录,名称必须叫webapp

springBoot访问静态资源

把上一个项目的static目录下的内容复制过来

springBoot访问静态资源

 同时把app.java类也复制过来

springBoot访问静态资源

 运行app.java类

springBoot访问静态资源
springBoot访问静态资源