SpringBoot跳转界面:静态和动态访问static和templates目录下的文件

一、新建一个SpringBoot项目

1、导入相关依赖(此处省略)

2、项目目录

在这里插入图片描述
可以看到resources下有两个文件夹static和templates,分别存放项目的静态和动态文件

二、static目录

1、创建一个staticfirst.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <h1>For the static files</h1>
    <h2>my first SpringBoot</h2>
</body>
</html>

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.1通过网址进行访问

http://localhost:8089/staticfirst.html

    
    
  • 1
注意:这里根据自己设置的端口号进行访问,一般默认为8080。若不确定可以在控制台查看输出,如下图

在这里插入图片描述

在浏览器中进行访问:

在这里插入图片描述

1.2、通过接口访问

创建一个controller包编写一个HelloController控制类

在这里插入图片描述

HelloController控制类:
package com.tracy.springbootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • Created by Tracy on 2020/3/20 12:23
    */
    @Controller
    public class HelloController {

    @RequestMapping(“hello”)
    public String toHello() {
    return “staticfirst.html”;
    }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在浏览器中发起hello请求:

http://localhost:8089/hello

 
 
  • 1

在这里插入图片描述
访问成功!

三、使用thymeleaf动态访问

1.1、pom.xml中导入thymeleaf的相关依赖

在pom.xml的dependencies中写入如下代码:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

 
 
  • 1
  • 2
  • 3
  • 4
接下来properties中设置thymeleaf相关属性
<!--   布局功能支持程序,thymeleaf3版本以上 layout2以上     -->
       <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
       <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
  • 1
  • 2
  • 3
  • 4

1.2、在templates下创建thymfirst.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <h1>For the thymeleaf</h1>
    <h2>my first SpringBoot</h2>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1.3、编写HelloController类

package com.tracy.springbootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • Created by Tracy on 2020/3/20 12:23
    */
    @Controller
    public class HelloController {

// @RequestMapping(“hello”)
// public String toHello() {
// return “staticfirst.html”;
// }

<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">toHello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    <span class="token keyword">return</span> <span class="token string">"thymfirst"</span><span class="token punctuation">;</span>   <span class="token comment">//注意:通过thymeleaf访问的界面不应添加后缀</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

1.4、动态访问界面

方法一:

继续在浏览器器中发起以下请求:

http://localhost:8089/hello

 
 
  • 1

访问结果:
在这里插入图片描述
发起请求成功!

方法二:

I、在容器中添加组件
创建config包编写MySpringBootConfig类,并实现WebMvcConfigurer接口

一般情况下我们访问界面都是访问根目录,或者是根目录下的index.html文件,所以这里将这两个请求加入组件容器中:

package com.tracy.springbootdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**

  • Created by Tracy on 2020/3/20 12:26
    */
    @Component
    public class MySpringBootConfig implements WebMvcConfigurer {

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurer() {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    //通过根目录进行访问,跳转至thymfirst.html界面
    registry.addViewController("/").setViewName(“thymfirst”);
    //通过index.html访问,仍然跳转至thymfirse.html界面
    //注意SetViewName方法就是访问的动态界面名,所以不添加后缀
    registry.addViewController(“index.html”).setViewName(“thymfirst”);
    }
    };
    }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
II、判断组件是否成功添加到容器中

在这里插入图片描述

package com.tracy.springbootdemo;

import org.springframework.context.ApplicationContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootDemoApplicationTests {

<span class="token annotation punctuation">@Autowired</span>
ApplicationContext ioc<span class="token punctuation">;</span>

<span class="token annotation punctuation">@Test</span>
<span class="token keyword">void</span> <span class="token function">contextLoads</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    <span class="token comment">//检测是否含有该组件</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>ioc<span class="token punctuation">.</span><span class="token function">containsBean</span><span class="token punctuation">(</span><span class="token string">"webMvcConfigurer"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
注意:我们向容器中添加的组件的名字就是添加组件的函数名。例如,我们刚刚通过编写webMvcConfigurer方法向容器中添加了组件,所以这里直接检查容器中是否含有该组件

在这里插入图片描述

运行结果:

在这里插入图片描述

返回为true,添加组件成功!
|||、访问界面

在浏览器中直接访问端口号:

http://localhost:8089/

 
 
  • 1

在这里插入图片描述

访问成功!

接下来再访问index.html界面:

http://localhost:8089/index.html

 
 
  • 1

在这里插入图片描述
访问成功!

四、总结

1、静态文件的访问默认是在static目录下的文件,如果是通过编写控制类来访问,则必须在返回值后添加文件类型后缀
2、动态文件的访问默认是在templates目录下访问,且应在pom.xml中导入thymeleaf依赖。如果此时编写控制类访问,则会覆盖静态文件的路径,直接访问templates下的文件。
3、templates下的文件不能通过文件名直接进行访问,必须发起请求(编写控制类),或者向系统容器中添加相应的组件。并且添加组件的方式可以改变访问路径。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值