zoukankan      html  css  js  c++  java
  • 20. SpringBoot 默认访问首页 以及 加载静态资源

    index 页面是个登录页面 ,现在它位于template文件夹下,但是template文件夹是被   Thymeleaf模板  解析的,并不是静态,直接访问不了,所以我们就得 配置控制器或者用拓展功能写请求视图:

     配置控制器返回视图:

    package com.bihu.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class userController {
        
        //配置的是/ 和 /index 映射
        @RequestMapping(value = {"/","/index"})
        public String login(){
            return "index";
        }
    
    }
    控制器

    拓展mvc实现:

    package com.bihu.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    
    @Configuration
    public class mConfig extends WebMvcConfigurerAdapter {
        @Bean
        //因为全部的 WebMvcConfigurerAdapter 会一起用 所以这里加入组件即可。
        public WebMvcConfigurerAdapter addView(){
            WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter(){
                @Override
                public void addViewControllers(ViewControllerRegistry registry) {
                    registry.addViewController("/").setViewName("index");
                    registry.addViewController("/index").setViewName("index");
                }
            };
            return webMvcConfigurerAdapter;
        }
    
    }
    配置类 拓展MVC 功能添加视图

     Bootstarp 视图加载不出,我们可以用thtmeleaf语法更改路劲:

    我们导入一个bootstarp ,然后改即可:

    我们还记的静态映射吗,反正是 webjars的请求都去 八分 INF 那边的目录找的 所以我们直接用 thtmeleaf 的 th:href 改link标签即可:

     

     然后:

    其实静态资源也可以直接访问哦 。。。

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15125794.html

  • 相关阅读:
    NVCC编译器
    BMP文件格式及读写
    Vim 命令 【转】
    NFS文件系统配置 和 GLIBC更新
    cuda vector addition
    hdu4758 Walk Through Squares 自动机+DP
    hdu4722 Good Numbers
    hdu4725 The Shortest Path in Nya Graph
    Anaconda-科学计算的 Python 发行版
    OUC_NewACMer_Personal_#1 题解
  • 原文地址:https://www.cnblogs.com/bi-hu/p/15125794.html
Copyright © 2011-2022 走看看