首页 > 编程语言 > 详细

20. SpringBoot 默认访问首页 以及 加载静态资源

时间:2021-08-10 23:37:03      阅读:27      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

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标签即可:

 

 技术分享图片

 

 然后:

技术分享图片

 

 

 

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

20. SpringBoot 默认访问首页 以及 加载静态资源

原文:https://www.cnblogs.com/bi-hu/p/15125794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!