spring boot 中的路径映射
2019-10-25 22:01
1441 查看
在spring boot中集成thymeleaf后,我们知道thymeleaf的默认的html的路径为classpath:/templates也就是resources/templates,那如何访问这个路径下面的静态页面呢?假设我们要访问一个页面为hello.html。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>hell spring boot!!</h1> </body> </html>
该页面位于templates下,当然也可以在application.properties文件中修改默认路径。
spring.thymeleaf.prefix=classpath:/templates
1.使用controller中的方法直接返回该页面
@Controller public class HelloController { @GetMapping("/hello") public String hello(){
//在集成thymeleaf后 会在默认路径下寻找名字为hello的html页面 return "hello"; } }
2.实现WebMvcConfigure接口中的addViewControllers方法进行路径的映射
@Configuration public class WebMvcConfig implements WebMvcConfigurer{ @Override public void addViewControllers(ViewControllerRegistry registry) { //第一个路径为类似于Controller中的接口的路径 第二个view为要访问的页面 //实现不需要进行数据渲染的页面的路径映射 当然这些页面没有在默认的五个静态页面访问路径下 registry.addViewController("/hopec").setViewName("hello");
//如果需要添加多个页面直接在下面继续添加即可 } }
相关文章推荐
- SpringBoot系列<四>视图解析、路径-资源映射、静态资源访问
- 项目案例: Springboot中使用过滤器映射访问路径
- SpringBoot重写WebMvcConfigurerAdapter 映射文件路径
- Spring Boot之配置虚拟映射路径
- SpringBoot重写addResourceHandlers映射文件路径
- Springboot 本地路径映射localhost路径
- SpringBoot设置虚拟路径映射绝对路径
- Springboot配置静态资源url映射及对应的本地路径
- springboot项目实现静态文件映射任意路径
- SpringBoot配置虚拟映射路径
- SpringBoot2.0 基础案例(03):配置系统全局异常映射处理
- SpringBoot学习_webjars和静态资源映射规则
- SpringBoot中通过配置文件控制路径和变量(三)
- springboot 获取项目绝对路径
- Spring boot 的静态资源映射映射《二》
- 解决Spring Boot中Multipartfile上传文件路径无效
- 第十一章:SpringBoot_Web开发——(静态资源映射规则)
- jar启动spring boot 获取jar的路径
- SpringBoot项目打包后,找不到文件路径问题,处理方法
- springboot构建映射平台