一、新建一个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
访问成功!