注意

  • 如果静态资源放到了静态资源文件夹下却无法访问,请检查一下是不是在自定义的配置类上加了@EnableWebMvc注解
  • templete文件夹不是静态资源的文件夹,默认是无法访问的,所以要添加视图映射
package cn.xxxxxx.hellospringbootweb.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("/index").setViewName("login");registry.addViewController("/index.html").setViewName("login");}
}

i18n国际化

  1. 编写国际化配置文件,抽取页面需要显示的国际化消息

    创建i18n文件夹存放配置文件,文件名格式为基础名(login)+语言代码(zh)+国家代码(CN)

  2. 在配置文件中添加国际化文件的位置和基础名,如果配置文件中没有配置基础名,就在类路径下找基础名为message的配置文件

    spring.messages.basename=i18n.login
    1. 点击切换语言    修改页面,点击连接携带语言参数
    <a class="btn btn-sm" href="?l=zh_CN">中文</a>
    <a class="btn btn-sm" href="?l=en_US">English</a>

实现登陆功能

1,提供登陆的Controller

@Controller
public class UserController {@PostMapping("/user/login")public String login(@RequestParam String username, @RequestParam String password, HttpSession session, Model model) {if (!StringUtils.isEmpty(username) && "123456".equals(password)) {//登录成功,把用户信息方法哦session中,防止表单重复提交,重定向到后台页面session.setAttribute("loginUser", username);return "redirect:/main.html";}//登录失败,返回到登录页面model.addAttribute("msg", "用户名或密码错误!");return "login";}
}

2,修改表单的提交地址,输入框添加name值与参数名称相对应

        <form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post"><img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1><label class="sr-only">Username</label><input type="text" name="username" class="form-control" th:placeholder="#{login.username}" placeholder="Username" autofocus=""><label class="sr-only">Password</label><input type="password" name="password" class="form-control" th:placeholder="#{login.password}" placeholder="Password" required=""><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"> [[#{login.remember}]]</label></div><button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button><p class="mt-5 mb-3 text-muted">© 2017-2018</p><a class="btn btn-sm" href="?l=zh_CN">中文</a><a class="btn btn-sm" href="?l=en_US">English</a></form>

3,由于登陆失败是转发,所以得修改静态资源的请求路径,在其中添加模版引擎

<link  href="asserts/css/bootstrap.min.css" th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">

4,添加登陆页面的显示,将msg传回主页面

<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<!--msg存在才显示该p标签-->
<p th:text="${msg}" th:if="${not #strings.isEmpty(msg)}" style="color: red"></p>

修改页面使其立即生效

在配置文件里面添加如下的命令,在页面修改完成之后,按快捷键ctrl+f9,重新编译

# 禁用缓存
spring.thymeleaf.cache=false

拦截器进行登陆检查

1,实现拦截器

package cn.xxxxxx.hellospringbootweb.interceptor;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class LoginHandlerInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {Object loginUser = request.getSession().getAttribute("loginUser");if (loginUser == null) {//未登录,拦截,并转发到登录页面request.setAttribute("msg", "您还没有登录,请先登录!");request.getRequestDispatcher("/index").forward(request, response);return false;}return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
}

2,注册拦截器

package cn.clboy.hellospringbootweb.config;import cn.clboy.hellospringbootweb.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class MyMvcConfig implements WebMvcConfigurer {//定义不拦截路径private static final String[] excludePaths = {"/", "/index", "/index.html", "/user/login", "/asserts/**"};@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("/index").setViewName("login");registry.addViewController("/index.html").setViewName("login");registry.addViewController("/main.html").setViewName("dashboard");}@Beanpublic LocaleResolver localeResolver() {return new MyLocaleResolver();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {//添加不拦截的路径,SpringBoot已经做好了静态资源映射,所以我们不用管registry.addInterceptor(new LoginHandlerInterceptor()).excludePathPatterns(excludePaths);}
}

注意:在spring2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截。但是在spring1.0+的版本中,是不会拦截静态资源的。因此我们需要将静态资源排除到拦截器的拦截路径之外

案例,实现员工的增删改查

实验功能 请求URI 请求方式
查询所有员工 emps GET
查询某个员工(来到修改页面) emp/1 GET
来到添加页面 emp GET
添加员工 emp POST
来到修改页面(查出员工进行信息回显) emp/1 GET
修改员工 emp PUT
删除员工 emp/1 DELETE
  1. 为了页面结构清晰,在template文件夹下新建emp文件夹,将list.html移动到emp文件夹下

  2. 将dao层和实体层java代码复制到项目中daoentities

  3. 添加员工controller,实现查询员工列表的方法

    @Controller
    public class EmpController {@Autowiredprivate EmployeeDao employeeDao;@GetMapping("/emps")public String emps(Model model) {Collection<Employee> empList = employeeDao.getAll();model.addAttribute("emps", empList);return "emp/list";}}
  4. 修改后台页面,更改左侧的侧边栏,并修改请求路径
    <li class="nav-item"><a class="nav-link" th:href="@{/emps}"><svg .....>......</svg>员工列表</a>
    </li>

thymeleaf公共页面元素抽取(参考官方文档)

  • ~{templatename::selector}:模板名::选择器
  • ~{templatename::fragmentname}:模板名::片段名
/*公共代码片段*/
<footer th:fragment="copy">&copy; 2011 The Good Thymes Virtual Grocery
</footer>/*引用代码片段*/
<div th:insert="~{footer :: copy}"></di/*(〜{...}包围是完全可选的,所以上⾯的代码 将等价于:*/
<div th:insert="footer :: copy"></di

三种引入公共片段的th属性:

  • th:insert:将公共片段整个插入到声明引入的元素中
  • th:replace:将声明引入的元素替换为公共片段
  • th:include:将被引入的片段的内容包含进这个标签中

后台页面的抽取

1,将后台主页中的顶部导航栏作为片段,在list中引入

        <nav th:fragment="topbar" class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0"><a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Company name</a><input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search"><ul class="navbar-nav px-3"><li class="nav-item text-nowrap"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Sign out</a></li></ul></nav>

2,list.html

<body><div th:replace="dashboard::topbar"></div>......

3,使用选择器的方式抽取左侧边栏的代码,就是我们将不同html文件的公共部分抽取出来,作为一个模版,其余需要的html文件只需要引入即可使用。比如现在我们将名称为sidebar的模版放在dashboard.html里面,而在list.html里面进行复用。

<!--dashboard.html-->
<div class="container-fluid"><div class="row"><nav id="sidebar" class="col-md-2 d-none d-md-block bg-light sidebar" ......
<!--list.html-->
<div class="container-fluid"><div class="row"><div th:replace="dashboard::#sidebar"></div>......

4,显示员工数据,添加增删改按钮

        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2><button class="btn btn-sm btn-success">添加员工</button></h2><div class="table-responsive"><table class="table table-striped table-sm"><thead><tr><th>员工号</th><th>姓名</th><th>邮箱</th><th>性别</th><th>部门</th><th>生日</th><th>操作</th></tr></thead><tbody><tr th:each="emp:${emps}"><td th:text="${emp.id}"></td><td th:text="${emp.lastName}"></td><td th:text="${emp.email}"></td><td th:text="${emp.gender}==1?'男':'女'"></td><td th:text="${emp.department.departmentName}"></td><td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td><td><button class="btn btn-sm btn-primary">修改</button><button class="btn btn-sm btn-danger">删除</button></td></tr></tbody></table></div></main>

5,员工添加页面 add.html

......
<body>
<div th:replace="commons/topbar::topbar"></div><div class="container-fluid"><div class="row"><div th:replace="commons/sidebar::#sidebar(currentURI='emps')"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><form><div class="form-group"><label>LastName</label><input name="lastName" type="text" class="form-control" placeholder="zhangsan"></div><div class="form-group"><label>Email</label><input  name="email" type="email" class="form-control" placeholder="zhangsan@atguigu.com"></div><div class="form-group"><label>Gender</label><br/><div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="gender" value="1"><label class="form-check-label">男</label></div><div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="gender" value="0"><label class="form-check-label">女</label></div></div><div class="form-group"><label>department</label><select name="department.id" class="form-control"><option th:each="dept:${departments}" th:text="${dept.departmentName}" th:value="${dept.id}"></option></select></div><div class="form-group"><label>Birth</label><input name="birth" type="text" class="form-control" placeholder="zhangsan"></div><button type="submit" class="btn btn-primary">添加</button></form></main></div>
</div>
......

6,点击链接,跳转到添加页面

<a href="/emp" th:href="@{/emp}" class="btn btn-sm btn-success">添加员工</a>

7,EmpController添加映射方法

    @Autowiredprivate DepartmentDao departmentDao;@GetMapping("/emp")public String toAddPage(Model model) {//准备部门下拉框数据Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("departments",departments);return "emp/add";}

8, 修改页面遍历添加下拉选项

<select class="form-control"><option th:each="dept:${departments}" th:text="${dept.departmentName}"></option>
</select>

9,表单提交,添加员工

<form th:action="@{/emp}" method="post">
    @PostMapping("/emp")public String add(Employee employee) {System.out.println(employee);//模拟添加到数据库employeeDao.save(employee);//添加成功重定向到列表页面return "redirect:/emps";}

10,日期格式的修改

表单提交的格式必须是yyyy/MM/dd的格式,可以在配置文件中修改格式

spring.mvc.date-format=yyyy-MM-dd

员工修改

  1. 点击按钮跳转到编辑页面

     <a th:href="@{/emp/}+${emp.id}" class="btn btn-sm btn-primary">修改</a>
  2. 添加编辑页面,将表单的提交方式设置为post方式,提供_method参数
    <body>
    <div th:replace="commons/topbar::topbar"></div><div class="container-fluid"><div class="row"><div th:replace="commons/sidebar::#sidebar(currentURI='emps')"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><form th:action="@{/emp}" method="post"><!--员工id--><input type="hidden" name="id" th:value="${emp.id}"><!--http请求方式--><input type="hidden" name="_method" value="put"><div class="form-group"><label>LastName</label><input name="lastName" th:value="${emp.lastName}" type="text" class="form-control" placeholder="zhangsan"></div><div class="form-group"><label>Email</label><input  name="email" th:value="${emp.email}" type="email" class="form-control" placeholder="zhangsan@atguigu.com"></div><div class="form-group"><label>Gender</label><br/><div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp.gender==1}"><label class="form-check-label">男</label></div><div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp.gender==0}"><label class="form-check-label">女</label></div></div><div class="form-group"><label>department</label><select name="department.id" class="form-control"><option th:each="dept:${departments}" th:value="${dept.id}" th:selected="${dept.id}==${emp.department.id}" th:text="${dept.departmentName}"></option></select></div><div class="form-group"><label>Birth</label><input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${#dates.format(emp.birth,'yyyy-MM-dd')}"></div><button type="submit" class="btn btn-primary">添加</button></form></main></div>
    </div>......
  3. controller转发到编辑页面,回显员工信息
        @GetMapping("/emp/{id}")public String toEditPage(@PathVariable Integer id, Model model) {Employee employee = employeeDao.get(id);//准备部门下拉框数据Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("emp", employee).addAttribute("departments", departments);return "emp/edit";}
  4. 提交表单修改员工的信息
        @PutMapping("/emp")public String update(Employee employee) {employeeDao.save(employee);return "redirect:/emps";}

员工删除

  1. 点击删除提交发出delete请求

        @DeleteMapping("/emp/{id}")public String delete(@PathVariable String id){employeeDao.delete(id);return "redirect:/emps";}
  2. 如果提示不支持POST请求,在确保代码无误的情况下查看是否配置启动HiddenHttpMethodFilter
  3. 这个好像是2.0版本以后修改的
    spring.mvc.hiddenmethod.filter.enabled=true

  4. 如果删除不掉,请修改EmployeeDao,把String转为Integer类型

        public void delete(String id) {employees.remove(Integer.parseInt(id));}

restful风格的增删改查相关推荐

  1. ssm框架restful风格实现增删改查

    1.什么是restful风格 大家在做Web开发的过程中,method常用的值是get和post. 可事实上,method值还可以是put和delete等等其他值. 既然method值如此丰富,那么就 ...

  2. 基于 Spring Boot 的 Restful 风格实现增删改查

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  3. 进入全屏 nodejs+express+mysql实现restful风格的增删改查示例

    首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...

  4. springmvc-实现增删改查

    30. 尚硅谷_佟刚_SpringMVC_RESTRUL_CRUD_显示所有员工信息.avi现在需要使用restful风格实现增删改查,需要将post风格的请求转换成PUT 请求和DELETE 请求 ...

  5. boot spring 接口接收数据_基于 Spring Boot 实现 Restful 风格接口,实现增删改查功能...

    优质文章,及时送达 Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配 ...

  6. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 作者:虚无境 cnblogs.com/xuwujing/p/8260935.html 前言 在去 ...

  7. 如何基于Restful ABAP Programming模型开发并部署一个支持增删改查的Fiori应用

    Jerry之前的文章30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用 发布之后,有朋友问我,"没错, 我是在你的文章里看到了Fiori应用的 ...

  8. 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用

    2016年时,Jerry曾经写过一系列关于SAP Fiori Smart Template(现在更名为Fiori Elements了)的博客,介绍了所谓的MDD开发方法论 - Metadata Dri ...

  9. springboot增删改查案例_大神基于SpringBoot开发一个Restful服务,实现增删改查功能...

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

最新文章

  1. [数字技巧]最大连续子序列和
  2. [思考]-32位的应用程序和64位的应用程序有什么区别
  3. Luogu3732 [HAOI2017] 供给侧改革 【后缀数组】【线段树】【乱搞】
  4. excel利用countif/match/lookup函数对比分析数据
  5. pc-bsd安装教程_桌面用户的BSD:PC-BSD的回顾
  6. 手机距离传感器坏了有什么影响_恢复出厂设置对手机有什么影响?
  7. 【caffe-windows】 caffe-master 之Matlab中model的分类应用
  8. Mongodb启动关闭
  9. hadoop2.6分布式环境搭建
  10. 读书感悟之,从术到道
  11. Robocode 直线瞄准机器人
  12. 如何将本地文件上传到Gitlab中?
  13. 我的Python心路历程 第十期 (10.10 股票实战可视化历史趋势)
  14. html5做在线课件,HTML5教程:制作移动教育课件
  15. java计算机毕业设计bs架构实习管理系统MyBatis+系统+LW文档+源码+调试部署
  16. 浏览器被劫持怎么办,详细讲解浏览器DNS被劫持的解决方法
  17. 重构第26天 移除双重否定(Remove Double Negative)
  18. 2021.5.5笔记 多态
  19. 计算机知识普及的策划,普及安全用电知识志愿者活动策划书
  20. pcm格式封装wav

热门文章

  1. java 在已有的so基础上封装jni_[干货]再见,Android JNI 封装
  2. 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案
  3. [Sharepoint2007对象模型]第三回:Web应用程序(SPWebApplication)
  4. CCIE理论-第五篇-SDA-2
  5. CCNA-第八篇-OSPF-上
  6. 从零开始学视觉Transformer(6):Swin Transformer-1
  7. springboot2整合mysql5_SpringBoot2.X (二十五):SpringBoot整合 Mybatis + MySQL CURD 示例
  8. go linux 源码编译环境,修改并编译golang源码
  9. mysql in优化_MySQL的一次优化记录 (IN子查询和索引优化)
  10. java 截串_java字符串截取