有三种方式可以实现访问首页:

第一种:

定义一个Controller,定义请求方法和返回内容,方法内容如下:

@RequestMapping({"/","/index"})
public String index(){
return "login";
}

即在访问http://localhost:8080/或者http://localhost:8080/index的时候,都会调用该index()方法,

该方法返回字符串login,由于我们在项目中pom.xml中配置了thymeleaf模板引擎,所以会解析扎到

classpath:/templates/index.html文件

第二种:

定义一个配置类,类上注解@Configuration,类实现接口WebMvcConfigurer,重写addViewControllers(ViewControllerRegistry registry)方法,代码如下:

package com.myself.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.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("login");
}
}
即在访问http://localhost:8080/index的时候,会定位到classpath:/templates/index.html文件,
记住classpath:/templates是thymeleaf模板引擎根据方法返回的字符串找对应的.html的路径

第三种

在配置类中,写一个方法返回WebMvcConfigurerAdapter类,并将该类用@Bean注解以组件的形式交给容器管理,

此种形式为推荐形式,代码如下:

  

package com.myself.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.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//使用WebMvcConfigurerAdapter来扩展SpringMVC的功能
//所有的WebMvcConfigurerAdapter都会生效
//注意要写在标有@Configuration的类中,要在方法上标上@Bean注解
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/helloIndex").setViewName("login");
registry.addViewController("/index").setViewName("login");
registry.addViewController("/").setViewName("login");
}
};
return webMvcConfigurerAdapter;
} }

另外在login.html中会引用一些css文件,一些是自己定义的,一些可能是bootstrap中的,其中,bootstrap起步依赖可在webjars中

找到对应的maven依赖,然后放入pom.xml中,代码如下:

a、webjars形式引用静态资源
<!--通过webjars中的找到bootstrap的maven依赖-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</dependency>

引入的bootstrap结构如下图:

其中红框1为webjars默认寻找的路径,红框2为我们用某个静态文件需要额外加路径

b、引用自己定义的静态资源文件

我们自己定义的静态资源文件如下图:

其中红框1为springboot默认寻找静态资源文件的路径,红框2 是自己需要额外加的路径。

c、html中引用静态资源文件的写法如下:

其中/webjars会去META-INF/resources/webjars下寻找  
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet"> 其中/会去"classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" 下寻找
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">

如有理解不到之处,望指正!

0012SpringBoot访问首页的更多相关文章

  1. 怎么修改tomcat默认访问首页

    一般情况下安装好tomcat之后我们的默认访问首页是index了,但我们如果要修改或增加一个默认首页,我们可参考下面办法来解决. 通过 ip:port 访问到的是 tomcat 的管理页面,其他常规部 ...

  2. nginx thinkphp只能访问首页

    代码部署到了服务器上,发现无论怎样请求,都是跳转到index/index/index(模块/控制器/方法),最后需要nginx重新地址即可 参考:Linux下Nginx部署Thinkphp5访问任何地 ...

  3. nginx报错:403 Forbidden 并且访问首页index.php是下载文件的状态

    nginx报错:403 Forbidden 并且访问首页index.php是下载文件的状态,不能正常解析php 系统有其他两个站访问是正常的 看日志没有看到明显的错误 搜索了下: 答案如下: php的 ...

  4. 4_3.springboot2.x之默认访问首页和国际化

    1.默认访问首页 1.引入thymeleaf和引入bootstrap <!--引入thymeleaf--> <dependency> <groupId>org.sp ...

  5. Docker++:docker运行Tomcat后访问首页报404 (永久解决方式)

    docker运行Tomcat后访问首页报404 与 tomcat 版本有关. 解决方式如下: 1.查看防火墙问题 2.Tomcat 下如果有 webapps.dist 和 webapps 则需要进行合 ...

  6. 设置java web工程中默认访问首页的几种方式

    1.demo中最常见的方式是在工程下的web.xml中设置(有时候根据业务可能需要设置action,在action中处理逻辑加载跳转什么的,比较少): <welcome-file-list> ...

  7. tomcat配置访问日志,访问首页主目录

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" ...

  8. Jsp 国际化访问首页选择展示不同字体小例子

    要求:创建一个首页,默然显示英文信息,但可以让用户选择使用英文,繁体中文或简体中文. 1.编写hello_en_US.txt,内容如下: cc.openhome.welcome=welcomecc.o ...

  9. phpstudy搭建网站只能访问首页,其他路由访问404

    今天博主遇到了一个很奇葩的问题,电脑下载了一个phpstudy搭建网站,框架用的是tp,但是除了输入域名能访问,其他页面都访问不了 经过博主的疯狂问大佬,以及百度,终于解决了这个问题 这次出现问题的原 ...

随机推荐

  1. Ubuntu下搜狗拼音输入法打不出汉字的解决方法

    问题 (1)Ubuntu下,搜狗拼音输入法能启动(系统托盘处有图标),但是打不出汉字,打字时选框不正常. 或者 (2)Deepin下,搜狗输入法无法启动,托盘处不显示图标,fcitx运行正常(这个可以 ...

  2. MySQL初始化脚本mysql_install_db使用简介及选项参数

    mysql_install_db是一个默认放在.../mysql/scripts的一个初始化脚本. 该脚本可以在任何装有perl的操作系统上被使用,在5.6.8之前的版本,该脚本是一个shell脚本, ...

  3. 基于 Spring + Atomikos + Mybatis的多数据源配置(含有BaseDao,BaseService)

    1.spring配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  4. oracle 查看表空间以及日志文件等系统文件

    --1.查看表空间的名称及大小 )), ) ts_size FROM dba_tablespaces t, dba_data_files d WHERE t.tablespace_name = d.t ...

  5. C语言位操作中指定的某一位数置0、置1、取反

    一.指定的某一位数置1 宏 #define setbit(x,y)  x|=(1<<y) 二.指定的某一位数置0 宏  #define clrbit(x,y)  x&=~(1< ...

  6. pthread_mutexattr_t设置的相关函数及其说明

    基本概述 该函数用于C函数的多线程编程中,互斥锁的初始化. 头文件:#include <pthread.h> 函数原型: int pthread_mutex_init(pthread_mu ...

  7. [转帖]Linux命令pmap

    Linux命令pmap https://www.cnblogs.com/lnlvinso/p/5272771.html jmap可以查看Java程序的堆内存使用情况,pmap可以查看Linux上运行的 ...

  8. Visual studio 2010(VS2010) 安装MSDN方法

    首先保证VS2010已经安装完毕 1.解压VS2010的安装文件(ISO),会看到ProductDocumentation文件夹,该文件夹下即为MSDN. 2.启动vs2010,点击"帮助& ...

  9. 利用Python进行数据分析_Pandas_绘图和可视化_Matplotlib

    1 认识Figure和Subplot import matplotlib.pyplot as plt matplotlib的图像都位于Figure对象中 fg = plt.figure() 通过add ...

  10. css中字体常用单位px、em、rem和%的区别及用法总结

    一.px.em.rem和%的定义 1.px(像素) px单位的名称为像素,它是一个固定大小的单元,像素的计算是针对(电脑/手机)屏幕的,一个像素(1px)就是(电脑/手机)屏幕上的一个点,即屏幕分辨率 ...