0012SpringBoot访问首页
有三种方式可以实现访问首页:
第一种:
定义一个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访问首页的更多相关文章
- 怎么修改tomcat默认访问首页
一般情况下安装好tomcat之后我们的默认访问首页是index了,但我们如果要修改或增加一个默认首页,我们可参考下面办法来解决. 通过 ip:port 访问到的是 tomcat 的管理页面,其他常规部 ...
- nginx thinkphp只能访问首页
代码部署到了服务器上,发现无论怎样请求,都是跳转到index/index/index(模块/控制器/方法),最后需要nginx重新地址即可 参考:Linux下Nginx部署Thinkphp5访问任何地 ...
- nginx报错:403 Forbidden 并且访问首页index.php是下载文件的状态
nginx报错:403 Forbidden 并且访问首页index.php是下载文件的状态,不能正常解析php 系统有其他两个站访问是正常的 看日志没有看到明显的错误 搜索了下: 答案如下: php的 ...
- 4_3.springboot2.x之默认访问首页和国际化
1.默认访问首页 1.引入thymeleaf和引入bootstrap <!--引入thymeleaf--> <dependency> <groupId>org.sp ...
- Docker++:docker运行Tomcat后访问首页报404 (永久解决方式)
docker运行Tomcat后访问首页报404 与 tomcat 版本有关. 解决方式如下: 1.查看防火墙问题 2.Tomcat 下如果有 webapps.dist 和 webapps 则需要进行合 ...
- 设置java web工程中默认访问首页的几种方式
1.demo中最常见的方式是在工程下的web.xml中设置(有时候根据业务可能需要设置action,在action中处理逻辑加载跳转什么的,比较少): <welcome-file-list> ...
- tomcat配置访问日志,访问首页主目录
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" ...
- Jsp 国际化访问首页选择展示不同字体小例子
要求:创建一个首页,默然显示英文信息,但可以让用户选择使用英文,繁体中文或简体中文. 1.编写hello_en_US.txt,内容如下: cc.openhome.welcome=welcomecc.o ...
- phpstudy搭建网站只能访问首页,其他路由访问404
今天博主遇到了一个很奇葩的问题,电脑下载了一个phpstudy搭建网站,框架用的是tp,但是除了输入域名能访问,其他页面都访问不了 经过博主的疯狂问大佬,以及百度,终于解决了这个问题 这次出现问题的原 ...
随机推荐
- QtCreator中打开.ui文件时卡死崩溃的解决方法
问题 QtCreator中打开一个项目,在编辑器中打开普通的.cpp或.h等文件正常,构建正常,运行正常,但是打开ui文件(QtCreator自动调用designer)时会卡死然后崩溃退出. 解决方法 ...
- 什么是挂载(mount)?
官方文档是这么解释的: All files accessible in a Unix system are arranged in one big tree, the file hierarchy, ...
- ByteDance面试
1.HashMap.HashSet源码解读 2.Http状态码.包头内容有哪些 响应头 说明 示例 状态 Access-Control-Allow-Origin 指定哪些网站可以跨域源资源共享 Acc ...
- 使用django进行大文件的上传下载
下载 基于Django建立的网站,如果提供文件下载功能,最简单的方式莫过于将静态文件交给Nginx等处理,但有些时候,由于网站本身逻辑,需要通过Django提供下载功能,如页面数据导出功能(下载动态生 ...
- 日常工作问题解决:rhel7下配置多路径设备
目录 1.情景描述 2.安装多路径软件 2.1 安装多路径 2.2检查安装情况 2.3 重启系统 2.4 将多路径软件添加至内核模块 2.5 检查内核添加情况 2.6 启动multipath服务 2. ...
- 学习笔记:oracle学习一:oracle11g体系结构之服务器结构、数据字典
目录 1.服务器架构 1.1 系统全局区SGA 1.1.1 高速数据缓冲区(database buffer cache) 1.1.2 重做日志缓冲区(redo log buffer cache) 1. ...
- EventLoop介绍
在Netty中使用EventLoop接口代表事件循环,EventLoop是从EventExecutor和ScheduledExecutorService扩展而来,所以可以讲任务直接交给EventLoo ...
- LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal
题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...
- Python开发【第五章】:常用模块
一.模块介绍: 1.模块定义 用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质上就是.py结尾python文件 分类:内置模块.开源模块.自定义模块 2.导入模块 本质:导 ...
- 小程序文案过长,‘收起/展开’文字,createSelectorQuery 获取节点成功,boundingClientRect 返回信息null
问题描述: wxml中id是动态生成的. 获取节点信息是在onReady生命周期函数内延时500ms执行的,select(id)可以获取全部节点信息,boundingClientRect (rect) ...