我是靠谱客的博主 贪玩草莓,这篇文章主要介绍SpringBoot之RestfulCRUDRestful风格的CRUDThymeleaf的使用Restful风格的CRUD,现在分享给大家,希望可以做个参考。

刚入门SpringBoot,通过一个入门案例来学习SpringBoot里的一些基础使用。

Restful风格的CRUD

项目目录如下,使用IDEA的Spring Initializr创建工程,只添加web模块
这里写图片描述
该项目使用的技术:
1、SpringBoot(web模块),默认自动配置
2、Thymeleaf模板引擎
3、Restful风格

该项目实现的功能:
1、静态资源访问
2、国际化
3、拦截器
3、登录
4、列表数据的增删查改

这里只记录一些重要的地方,一些简单的不做介绍

《本项目源码下载》

全局配置文件application.properties

复制代码
1
2
3
4
5
6
7
8
9
10
#服务器端口 server.port=8088 #静态资源 #spring.resources.static-locations=classpath:/static/,classpath:/resources/,classpath:/webjars/ #指定国际化文件 spring.messages.basename=i18n.login #开发时禁用thymeleaf缓存 spring.thymeleaf.cache=false #日期格式化 spring.mvc.date-format=yyyy-MM-dd

自定义配置类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 加入自定义组件,扩展SpringMVC的功能 //@EnableWebMvc // 如果在配置类上加上此注解,则SpringBoot对SpirngMvc的自动配置失效 @Configuration public class MyMvcConfigurer implements WebMvcConfigurer { // 添加视图控制器,完成页面资源映射 @Override public void addViewControllers(ViewControllerRegistry registry) { // 配置路径映射 registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); registry.addViewController("/login.html").setViewName("login"); registry.addViewController("/main.html").setViewName("dashboard"); } // 添加拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginHandlerInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/", "/login.html", "/user/login"); } // 加入国际化组件 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }

Thymeleaf的使用

引入命名空间:

复制代码
1
<html lang="en" xmlns:th="http://www.thymeleaf.org">

把需要渲染的模板放到/templates目录下,以html后缀命名,就会被自动渲染

由于页面的顶部栏和左侧栏是通用的,所以将其抽取出来放在bar.html中,然后使用Thymeleaf标签在相应位置引入即可

Restful风格的CRUD

URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作

Tables普通CRUD(uri来区分操作)RestfulCRUD
查询getEmpemp—GET
添加addEmp?xxxemp—POST
修改updateEmp?id=xx&xxx=xxxemp/{id}—PUT
删除deleteEmp?id=xemp/{id}—DELETE

本次实验的请求架构:

实验功能请求URI请求方式
查询所有员工empsGET
来到添加页面empGET
添加员工empPOST
来到修改页面(查出员工进行信息回显)emp/{id}GET
修改员工empPUT
删除员工emp/{id}DELETE

问题:由于普通的form表单只支持GET和POST方式,可以使用一种方式来解决这个问题
方法:
1、SpringMVC中配置HiddenHttpMethodFilter(SpringBoot自动配置好的)
2、页面创建一个post表单
3、创建一个input项,name=”_method”,值就是我们指定的请求方式

html

复制代码
1
2
3
4
5
6
7
8
9
10
<form action="/emp" method="post"> <!--发送put请求修改员工数据--> <!-- 1、SpringMVC中配置HiddenHttpMethodFilter(SpringBoot自动配置好的) 2、页面创建一个post表单 3、创建一个input项,name="_method",值就是我们指定的请求方式 --> <input type="hidden" name="_method" value="put"/> ...... </form>

Controller

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 修改员工信息 @PutMapping("/emp") public String edit(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; } // 根据id查出员工信息,跳转到修改页面进行回显 @GetMapping("/emp/{id}") public String toEditPage(@PathVariable("id")Integer id, Model model){ // 根据id查出员工信息 Employee employee = employeeDao.get(id); // 获取部门信息,用于页面回显 Collection<Department> departments = departmentDao.getDepartments(); model.addAttribute("deps", departments); model.addAttribute("emp", employee); return "emp/add"; // 这里addPage和editPage共同使用一个页面 }

其他的提交方式以此类推。

最后

以上就是贪玩草莓最近收集整理的关于SpringBoot之RestfulCRUDRestful风格的CRUDThymeleaf的使用Restful风格的CRUD的全部内容,更多相关SpringBoot之RestfulCRUDRestful风格内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部