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

第一种:

定义一个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. 【miscellaneous】GStreamer下的音视频播放

    Gst-launch命令: gst-launch filesrc location=*.* ! demux name=demux demux.video_00 ! queue ! decoder ! ...

  2. vue中的$attrs属性和inheritAttrs属性

    一.vue中,默认情况下,调用组件时,传入一些没有在props中定义的属性,会把这些“非法”属性渲染在组件的根元素上(有一些属性除外),而这些“非法”的属性会记录在$attrs属性上. 二.如何控制不 ...

  3. elasticsearch7.1.1【win】下载安装

    下载:https://www.elastic.co/cn/downloads/elasticsearch 历史版本下载:https://www.elastic.co/cn/downloads/past ...

  4. [转帖]彻底弄懂UTF-8、Unicode、宽字符、locale

    彻底弄懂UTF-8.Unicode.宽字符.locale linux后端开发   已关注   彻底弄懂UTF-.Unicode.宽字符.locale unicode 是字符集 utf-8是编码格式.. ...

  5. linux shell中的EOF

    关键词:EOF 在平时的运维工作中,我们经常会碰到这样一个场景:执行脚本的时候,需要往一个文件里自动输入N行内容.如果是少数的几行内容,还可以用echo追加方式,但如果是很多行,那么单纯用echo追加 ...

  6. SQL-TSQL

    一.系统存储过程 常用  sp_helptext --查看可编程性(存储过程.函数.触发器.规则.默认值),表中(约束.触发器) EXEC sp_helptext f_M_Student 二.全局变量 ...

  7. java项目报错 :A class file was not written. The project may be inconsistent...

    问题: 打开ecplise,发现我的几个项目报错,上午还用的好好的,整我一脸懵,出现那么多错误还都是一种问题,错误提示翻译过来是:(类文件找不到) : 问题经过具体描述: 不只是在我的springMV ...

  8. SrpingBoot入门到入坟03-基于idea快速创建SpringBoot应用

    先前先创建Maven项目然后依照官方文档再然后编写主程序写业务逻辑代码才建立好SpringBoot项目,这样太过麻烦,IDE都支持快速创建,下面基于idea: 使用Spring Initializer ...

  9. linux下安装lnmp集成环境

    linux下安装lnmp集成环境 教程地址:https://www.cnblogs.com/peteremperor/p/6750204.html 必须要用root用户,否则权限不够无法安装 安装最新 ...

  10. 使用 jsvc 启动tomcat(使用普通用户运行)

    使用 jsvc 启动tomcat(使用普通用户运行) jsvc简介 在生产中,tomcat应该以daemon的模式运行,而且如果需要以普通用户的身份启动tomcat,那么就不能使用1024以下的端口, ...