11. SpringBoot 之CRUD实例
SpringBoot静态页路径,可直接通过URL访问的:
- /META-INF/resources
- /resources
- /static
- /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实例的更多相关文章
- springboot + mybatisPlus 入门实例 入门demo
springboot + mybatisPlus 入门实例 入门demo 使用mybatisPlus的优势 集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用ma ...
- springboot + kafka 入门实例 入门demo
springboot + kafka 入门实例 入门demo 版本说明 springboot版本:2.3.3.RELEASE kakfa服务端版本:kafka_2.12-2.6.0.tgz zooke ...
- Springboot+Mybatis+MySQL实例练习时踩坑记录
最近刚开始学习后端,直接让上手学习Springboot+Mybatis+MySQL对CRUD的实例,虽然实例不难,但是上面的三个知识我都不懂,就有点为难我了 所以经常遇到一个点卡自己很久的情况,这里列 ...
- 【SpringBoot】11.Springboot整合SpringMVC+Mybatis(上)
Springboot整合SpringMVC+Mybatis 需求分析:通过使用Springboot+SpringMVC+Mybatis 整合实现一个对数据库表users表的CRUD操作. 1.创建项目 ...
- IDEA上创建 Maven SpringBoot + zookeeper +dubbo 实例
概述 首先声明,本文是学习总结类型的博客内容,如有雷同纯属学习.本位主要结合zookeeper和dubbo做个简单实例.目前来说,一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越 ...
- SpringBoot+Mybatis整合实例
前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...
- 11个rsync使用实例
rsync表示 remote sync,其用于在本地或与远程主机间进行文件或目录备份.相比较scp等工具,rsync有以下优点: 速度:除首次全拷贝外,其他时候实现增量拷贝,加快传输速度 安全:传输数 ...
- SpringBoot Restful Crud
一个简单的Restful Crud实验 默认首页的访问设置: // 注册 自定义的mvc组件,所有的WebMvcConfigurer组件都会一起起作用 @Bean public WebMvcConfi ...
- 补习系列(11)-springboot 文件上传原理
目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...
随机推荐
- python之count()函数
# count()统计字符串中特定单词或短语出现次数(n = 3) strs = 'Good! Today is good day! Good job!' n = strs.lower().count ...
- Freemarker空值判断
freemarker中显示某对象使用${name}. 但如果name为null,freemarker就会报错.如果需要判断对象是否为空: <#if name??> …… </#if& ...
- linux-shell系列7-ssh密匙生成分发
sshpass 该软件就是为ssh提供密码使用的yum install sshpass -y 批量分发脚本内容 #!/bin/bash. /etc/rc.d/init.d/functions # ...
- SpringBoot基础入门
1.SpringBoot核心相关内容 1.1入口类 SpringBoot通常有一个入口类*Application,内部有一个main方法,是启动SpringBoot的入口.使用@SpringBootA ...
- bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...
- MT【53】对数平均做数列放缩
[从最简单的做起]--波利亚 请看下面三道循序渐进不断加细的题. 评:随着右边的不断加细,解决问题的方法也越来越"高端".当然最佳值$ln2$我们可以用相对 容易的方法来证明: $ ...
- Nginx多进程高并发、低时延、高可靠机制在缓存(redis、memcache)twemproxy代理中的应用
1. 开发背景 现有开源缓存代理中间件有twemproxy.codis等,其中twemproxy为单进程单线程模型,只支持memcache单机版和redis单机版,都不支持集群版功能. 由于twemp ...
- 自学Linux Shell18.3-sed实用工具
点击返回 自学Linux命令行与Shell脚本之路 18.3-sed实用工具 1. 加倍行间距 命令格式: .......
- Python基础-简介一
一.Python介绍 1. Python的应用领域及流行程度 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间, ...
- [hihocoder1509][异或排序]
hihocoder1509 思路 对于每两个数,从二进制的高位到低位考虑,发现,若前面一个的当前位是1,后面一个的当前位置是0,那么s的当前位置必须是1.反之,若前面是0,后面是1,那么s的当前位置必 ...