SpringBoot静态页路径,可直接通过URL访问的:
  1. /META-INF/resources
  2. /resources
  3. /static
  4. /public

而    5. /template 只和模板引擎有关,加载了模板引擎才能用

welcome页面的设置默认为index.html

按照1,2,3,4,5的顺序寻找index.html,先找到的先解析,但是一般都放到templates目录下。

  

SpringBoot CRUD开发


1.将页面文件和类文件:SpringBoot_crud_页面.tar  加入到项目相应路径下:

实现方式


1. 只在/templates目录下创建index.html页面,什么也不配智,根据SpringBoot的自动配置自动匹配上

2. 在Controller中指定,但是此时Controller的类上不能配置@RequestMapping("/somePath"),因为页面引用的静态文件默认是在当前路径下,也会被加上/somePath,以至于找不到静态资源文件而无法渲染

  • @RequestMapping("/hello")
    @Controller
    public class HelloWorldController
    {
    /**
    * 这里会被模板引擎解析而走/templates/index.html
    * @return
    */
    @RequestMapping({"/","/index","/index.html"})
    public String index() {
    return "index";
    }
    }

  

3. 自定义配置SpringBoot web的配置类

@Configuration
public class MyConfig implements WebMvcConfigurer {
/**
* 浏览器发送addViewTest请求,来到success页面
* 发请求到页面,就没有必要在Controller里写空方法了,直接来做视图映射
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/addViewTest").setViewName("success");
registry.addViewController("/").setViewName("index");
}
}

即等同于SpringMvc的如下配置

<mvc:view-controller path="/" view-name="success"/><!-- 配置文件设置页面跳转,发一个请求到页面,就没必要写空方法了,配一下即可 -->

4. 配置文件类中@Bean注册WebMvcConfigurer对象实例

@Configuration
public class MyConfig implements WebMvcConfigurer { @Bean
public WebMvcConfigurer webMvcConfigurer() {
WebMvcConfigurer configurer = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
};
return configurer;
} }

二、引入BootStrap的webjars取代本地文件


1.maven依赖

<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>

2. 在yml文件中加入配种:

server:
servlet:
context-path: /crud //注意,值前面必须带斜杠

3.在页面源码中加入thymeleaf模板元素

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<!--
1.原生的href标签
引用值前面不能加斜杠:"/",表示相对路径,以下路径皆可识别
1)项目的/resources/static/路径
2)webjars的jar包中的/META-INF/resources/ 路径下,
2.thymeleaf模板引擎标签
2.1 值前面不➕斜杠,效果与原生的href无异,都是取相对路径
2.2 值前面➕斜杠,thymeleaf会在斜杠前自动加上系统的根路径
即配置文件中:server.servlet.context-path=/crud3 指定的值,不指定则默认为空
-->
<link href="webjars/bootstrap/4.1.3/css/bootstrap.css" th:href="@{/webjars/bootstrap/4.1.3/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head> <body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label class="sr-only">Username</label>
<input type="text" class="form-control" placeholder="Username" required="" autofocus="">
<label class="sr-only">Password</label>
<input type="password" class="form-control" placeholder="Password" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm">中文</a>
<a class="btn btn-sm">English</a>
</form> </body> </html>

11. SpringBoot 之CRUD实例的更多相关文章

  1. springboot + mybatisPlus 入门实例 入门demo

    springboot + mybatisPlus 入门实例 入门demo 使用mybatisPlus的优势 集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用ma ...

  2. springboot + kafka 入门实例 入门demo

    springboot + kafka 入门实例 入门demo 版本说明 springboot版本:2.3.3.RELEASE kakfa服务端版本:kafka_2.12-2.6.0.tgz zooke ...

  3. Springboot+Mybatis+MySQL实例练习时踩坑记录

    最近刚开始学习后端,直接让上手学习Springboot+Mybatis+MySQL对CRUD的实例,虽然实例不难,但是上面的三个知识我都不懂,就有点为难我了 所以经常遇到一个点卡自己很久的情况,这里列 ...

  4. 【SpringBoot】11.Springboot整合SpringMVC+Mybatis(上)

    Springboot整合SpringMVC+Mybatis 需求分析:通过使用Springboot+SpringMVC+Mybatis 整合实现一个对数据库表users表的CRUD操作. 1.创建项目 ...

  5. IDEA上创建 Maven SpringBoot + zookeeper +dubbo 实例

    概述 首先声明,本文是学习总结类型的博客内容,如有雷同纯属学习.本位主要结合zookeeper和dubbo做个简单实例.目前来说,一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越 ...

  6. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

  7. 11个rsync使用实例

    rsync表示 remote sync,其用于在本地或与远程主机间进行文件或目录备份.相比较scp等工具,rsync有以下优点: 速度:除首次全拷贝外,其他时候实现增量拷贝,加快传输速度 安全:传输数 ...

  8. SpringBoot Restful Crud

    一个简单的Restful Crud实验 默认首页的访问设置: // 注册 自定义的mvc组件,所有的WebMvcConfigurer组件都会一起起作用 @Bean public WebMvcConfi ...

  9. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

随机推荐

  1. 设计模式笔记:适配器模式(Adapter)

    1. 适配器模式简介 1.1 模式定义 适配器模式:通过一个类的接口转换成客户希望的另外一个接口,使原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器从结构上分为:类适配器和对象适配器.其 ...

  2. Delphi2007卸载后无法再安装

    Win7下卸载delphi2007后重新安装问题: 1.Delphi/C++ must be installed first 2.could not access network loacation/ ...

  3. The Shortest Statement CodeForces - 1051F(待测试)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  4. Codeforces Round #418 (Div. 2) B. An express train to reveries

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. MT【191】阿波罗尼乌斯圆

    已知$f(x)=2\sqrt{(\cos x+\frac{1}{2})^2+\sin^2 x}-\sqrt{\cos^2 x+(\sin x-\frac{1}{2})^2}$,若$m\ge f(x)$ ...

  6. 解析 Android Things 技术原理

    2012 年 6 月,由 IoT-GSI(Global Standards Initiative on Internet of Things)发布的白皮书“ITU-T Y.4000/Y.2060”[1 ...

  7. 01 自学Aruba之功率单位和相对单位

    点击返回:自学Aruba之路 01 自学Aruba之功率单位和相对单位 功率单位是用来测量传输振幅和接受振幅的大小,功率单位测量的是绝对功率 相对单位是用来计算增加电缆或天线后的损耗和增益的大小,相对 ...

  8. 自学Python2.8-条件(if、if...else)

    自学Python之路 自学Python2.8-条件(if.if...else) 1.if 判断语句 if语句是用来进行判断的,其使用格式如下:  if 要判断的条件: 条件成立时,要做的事情 当“判断 ...

  9. python多线程用法及与单线程耗时比较

    下面,通过一个简单的例子,来把多线程和单线程执行任务的耗时做个比较 import time import threading # 音乐播放器 def music(func, loop): for i ...

  10. Spring MVC 起步

    跟踪Spring MVC的请求 在请求离开浏览器时①,会带有用户所请求内容的信息,至少会包含请求的URL. 请求旅程的第一站是Spring的DispatcherServlet.与大多数基于Java的W ...