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. Jquery Ajax xml版Get请求PHP

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  2. JavaScript DOM方法表格添加删除

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  3. oracle小知识点

    一 . procedure和function: procedure和function在语法上几乎完全一样,使用上却有小小的差别, procedure可以单独的调用 在命令行直接exec pro_xxx ...

  4. Java之Array(数组)说明

    代码说明: package array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** ...

  5. Lodop打印语句最基本结构介绍(什么是一个任务)

    Lodop中最基本的打印过程至少有初始化语句.添内容语句和打印语句三部分组成,例如: LODOP.PRINT_INIT("打印任务名"); //首先一个初始化语句 LODOP.AD ...

  6. C# 网络请求

    C#自带网络请求 由于偶尔会用到服务端请求,写下随笔,以便下次使用 . var Url = "http://rueiqiang.vicp.net/api/Franchisee/QueryIn ...

  7. 将关系型数据库抽取成redis的思路

    思路是 先把id抽取出来形成一个·list表示数量 然后再把表变成键值对形式把id当做成键

  8. Django-website 程序案例系列-14 缓存的应用配置文件的写法

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  9. BZOJ1001 BJOI2006狼抓兔子(最小割+最短路)

    显然答案就是最小割.直接跑dinic也能过,不过显得不太靠谱. 考虑更正确的做法.作为一个平面图,如果要把他割成两半,那么显然可以用一条曲线覆盖且仅覆盖所有割边.于是我们把空白区域看成点,隔开他们的边 ...

  10. MT【20】一道三次函数的难题

    评:这道题由于系数弄得不是很好,涉及的难度为联赛一试+难度.中间用到了$Sturm$定理,还涉及到一些代 数变形技巧,最后一个求关于$m$的三次方程又涉及到三次方程的求法.一个小时讲这一道题也不为过.