zoukankan      html  css  js  c++  java
  • SpringBoot——路径映射

    在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("/hellp").setViewName("hello");
        //如果需要添加多个页面直接在下面继续添加即可
        }
    }
    
  • 相关阅读:
    navigator对象及属性(userAgent)(扩展)
    最新Visual C++ 运行时
    Wakfu .pk 音频文件提取
    Flex布局学习记录
    小程序 swiper bindChange 抖动解决方法
    小程序 scroll-view 无法触发 onReachBottom 解决办法
    小程序修改按钮宽高
    db.collection(变量名)
    小程序图片轮播自适应
    微信小程序 MD5引用
  • 原文地址:https://www.cnblogs.com/luckyhui28/p/12355342.html
Copyright © 2011-2022 走看看