SpringBoot服务端数据-实现添加用户功能
- 创建一个Maven的jar工程。

- 修改POM文件添加Web启动器与Thymeleaf坐标。

- 在项目中使用Thymeleaf编写一个添加用户的视图。
- <!DOCTYPE html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <form th:action="@{/addUser}" method="post">
- <p>姓名:<input type="text" name="name" th:field="${user.name}"><font color="red" th:errors="${user.name}"></font></p>
- <p>密码:<input type="text" name="password" th:field="${user.password}"><font color="red" th:errors="${user.password}"></font></p>
- <p>年龄:<input type="text" name="age" th:field="${user.age}"><font color="red" th:errors="${user.age}"></font></p>
- <p>邮箱:<input type="text" name="email" th:field="${user.email}"><font color="red" th:errors="${user.email}"></font></p>
- <p>性别:<select name="sex">
- <option value="男">男</option>
- <option value="女">女</option>
- </select><font color="red" th:errors="${user.sex}"></font>
- </p>
- <input type="submit" value="提交"><span th:text="${#httpServletRequest.getAttribute('msg')}"></span>
- </form>
- </body>
- </html>
- 创建一个Controller处理添加用户请求。
- package com.bjsxt.controller;
- import com.bjsxt.pojo.User;
- import com.bjsxt.service.OperateService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.RequestMapping;
- import javax.validation.Valid;
- import java.util.List;
- @Controller
- public class FuncationController {
- @Autowired
- private OperateService service;
- @RequestMapping("/findAll")
- public String findAll(Model model){
- List<User> list = service.findAll();
- model.addAttribute("users", list);
- System.out.println(list);
- return "index";
- }
- /**
- * @Valid:开启对pojo实体类的数据验证
- * BindingResult:封装了校验的结果
- * @param user
- * @return
- */
- @RequestMapping("/addUser")
- public String addUser(@Valid User user,
- BindingResult result) {
- /**
- * result.hasErrors():判断数据校验是否通过,通过返回false,否则返回true
- * The method be used to judge that if the data validate is approved,return false or true
- */
- if (result.hasErrors()) {
- System.out.println("验证");
- return "adduser";
- }
- int i = service.addUser(user);
- if(i==1){
- return "redirect:/findAll";
- }
- else {
- return "error";
- }
- }
- }
- SpringBoot服务端数据-数据校验
- Spring Boot中服务端数据校验技术的特点是什么?
SpringBoot中使用了Hibernate-validate校验框架
- 阐述Spring Boot中如何实现服务端数据校验?
1.在pojo实体类中添加校验规则的注解:
- package com.bjsxt.pojo;
- import lombok.Data;
- import org.hibernate.validator.constraints.Length;
- import javax.validation.constraints.*;
- @Data
- public class User {
- /**
- * 在实体类中添加校验规则
- * @NotBlank:非空校验(判断字符串是否为null或空串,去掉首尾的空格)
- * @NotEmpty:非空校验(判断字符串是否为null或空串,不会去掉首尾的空格)
- * @Length:指定字符串的长度,最小:min ,最大:max
- * @Min():指定数值类型最小值
- * @Max():指定数值类型最大值
- * @Email:验证邮箱格式
- * @NotNull():数值类型非空校验
- */
- private Integer id;
- @NotBlank(message = "用户名不能为空")
- private String name;
- @NotEmpty(message = "密码不能为空")
- @Length(min = 6,max = 12)
- private String password;
- @NotBlank(message = "性别不能为空")
- private String sex;
- @Min(value = 1,message = "年龄最小为1岁")
- @Max(value = 150,message = "年龄最大不能超过150岁")
- @NotNull(message = "年龄不能为空")
- private Integer age;
- @NotBlank(message = "邮箱不能为空")
- private String email;
- }
- Controller类中开启数据验证
- /**
- * @Valid:开启对pojo实体类的数据验证
- * BindingResult:封装了校验的结果
- * @param user
- * @return
- */
- @RequestMapping("/addUser")
- public String addUser(@Valid User user,
- BindingResult result) {
- /**
- * result.hasErrors():判断数据校验是否通过,通过返回false,否则返回true
- * The method be used to judge that if the data validate is approved,return false or true
- */
- if (result.hasErrors()) {
- System.out.println("验证");
- return "adduser";
- }
- int i = service.addUser(user);
- if(i==1){
- return "redirect:/findAll";
- }
- else {
- return "error";
- }
- }
- @NotBlank注解的作用是什么?
非空校验(判断字符串是否为null或空串,去掉首尾的空格)
- @Valid注解的作用是什么?
@Valid:开启对pojo实体类的数据验证
- BindingResult的作用是什么?
BindingResult:封装了校验的结果
- SpringBoot服务端数据-解决异常
- 在服务端数据校验时会出现什么异常?

- 产生异常的原因是什么?
页面跳转时,调用页面跳转的方法中没有User对象
- 如何解决该异常?
在跳转页面的方法中注入一个对象,要求参数对象的变量名必须是对象的类名的全称首字母小写。

- @ModelAttribute注解的作用是什么?
如果想为传递的对象更改名称,可以使用@ModelAttribute("aa")这表示当前传递的对象的key为aa。

- SpringBoot服务端数据-其他校验规则
- @NotBlank: 注解的作用是什么?
非空校验(判断字符串是否为null或空串,去掉首尾的空格)
- @NotEmpty: 注解的作用是什么?
非空校验(判断字符串是否为null或空串,不会去掉首尾的空格)
- @Length: 注解的作用是什么?
指定字符串的长度,最小:min ,最大:max
- @Min: 注解的作用是什么?
指定数值类型最小值
- @Max: 注解的作用是什么?
指定数值类型最大值
- @Email:注解的作用是什么?
验证邮箱格式
- SpringBoot异常处理-自定义错误页面
- 在Spring Boot中一共提供了几种处理异常的方式?
5种
- 什么是自定义错误页面方式?
SpringBoot 默认的处理异常的机制: SpringBoot 默认的已经提供了一套处理异常的机制。 一旦程序中出现了异常 SpringBoot 会像/error 的 url 发送请求。在 springBoot 中提供了一个 叫 BasicExceptionController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常 信息。
如 果 我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再 src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error
新建一个Thymeleaf页面作为自定义错误页面,命名必须为error.html
- 自定义错误页面的命名上有何要求?
命名必须为error.html
- 自义定错误页面应该放到项目的什么位置?
应该放在resources/templates目录下
- SpringBoot异常处理-@ExceptionHandler
- @ExceptionHandler注解的作用是什么?
@ExceptionHandler():指定处理的异常类型,当产生这些异常时,自动调用该方法
- 使用@ExceptionHandler注解处理异常的步骤是什么?
- /**
- * 方式二:@ExceptionHandler()注解
- *
- * @ExceptionHandler(value = {java.lang.NullPointerException.class})
- * 指定处理的异常类型,当产生这些异常时,自动调用该方法
- *
- * ModelAndView:封装异常信息以及视图的指定Mo
- *
- * Exception e:会将产生的异常对象注入该方法
- *
- * 缺点:1.如果处理的异常类型很多时,代码量会很多,代码冗余
- * 2.只能处理当前Controller,不具备跨Controller的能力
- * @param e
- * @return
- */
- @ExceptionHandler(value = {java.lang.NullPointerException.class})
- public ModelAndView nullPointerException(Exception e){
- ModelAndView mv=new ModelAndView();
- //将异常信息注入
- mv.addObject("error", e.toString());
- //指定视图:
- mv.setViewName("error");
- return mv;
- }
- SpringBoot异常处理-@ControlleAdvice
- @ControllerAdvice注解的作用是什么?
@ControllerAdvice注解修饰的异常处理类可以处理全局的异常
- @ControllerAdvice+@ExceptionHandler注解处理异常有什么特点?
1.如果处理的异常类型很多时,代码量会很多,代码冗余
2.只能处理当前Controller,不具备跨Controller的能力
- SpringBoot异常处理-SimpleMappingExceptionResolver
- SimpleMappingExceptionResolver的作用是什么?
全局异常处理类,使用SimpleMappingExceptionResolver 做全局异常处理
- 阐述使用SimpleMappingExceptionResolver处理异常的方式是什么?
- package com.bjsxt.exception;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
- import java.util.Properties;
- /**
- * 异常处理方式四:全局异常处理+处理异常方法简化
- * @Configuration+@Bean注解+SimpleMappingExceptionResolver对象
- * 缺点:不能传递异常信息
- */
- @Configuration
- public class GlobalExceptionHandle4 {
- /**
- * 该方法必须要有返回值:返回值类型为 SimpleMappingExceptionResolver
- * @return
- */
- @Bean
- public SimpleMappingExceptionResolver handleException(){
- SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver();
- Properties mappings=new Properties();
- /**
- * Properties类:
- * 参数一(key):表示异常的类型(异常类型的全名)
- * 参数二(value):表示要跳转的视图名称
- */
- mappings.put("java.lang.NullPointerException","error");
- mappings.put("java.lang.ArithmeticException","error2");
- resolver.setExceptionMappings(mappings);
- return resolver;
- }
- }
- SpringBoot异常处理-自定义HandlerExceptionResolver
- HandlerExceptionResolver接口的作用是什么?
异常统一处理接口
- 阐述使用HandlerExceptionResolver接口处理异常的方式是什么?
- package com.bjsxt.exception;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.HandlerExceptionResolver;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.Properties;
- /**
- * 异常处理方式五:全局异常处理+处理异常方法简化
- * @Configuration+实现HandlerExceptionResolver接口
- * 缺点:不能传递异常信息
- */
- @Configuration
- public class GlobalExceptionHandle5 implements HandlerExceptionResolver {
- @Override
- public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
- ModelAndView mv=new ModelAndView();
- if(e instanceof java.lang.NullPointerException){
- mv.setViewName("error");
- }else if(e instanceof java.lang.ArithmeticException){
- mv.setViewName("error2");
- }
- mv.addObject("error", e);
- return mv;
- }
- }
- Spring Boot整合junit单元测试
- @RunWith注解的作用是什么?
@RunWith():启动类,表示项目将从该类开始运行
- @SpringBootTest注解的作用是什么?
@SpringBootTest()表示当前类是SpringBoot的测试类,并加载SpringBoot的启动类
- Spring Boot热部署-SpringLoader-方式一
- 使用SpringLoader实现热部署的方式有几种?
两种
- SpringLoader实现热部署有什么缺陷?
- 该种方式只能对后端代码进行热部署,对前端代码无能为力
- 关闭需要在任务管理器结束进程
- 如何启动通过Maven插件方式引入SpringLoader实现热部署的服务?

- 如何关闭使用SpringLoader热部署的服务?
需要在任务管理器中关闭
- Spring Boot热部署-SpringLoader-方式二
- 手动添加SpringLoader的jar实现项目的热部署的步骤是什么?



- 启动服务时与基于插件方式添加SpringLoader方式有何区别?
基于插件方式需要使用Maven命令:spring-boot:run
基于jar包方式:

- 启动服务时需要添加什么启动参数?
-javaagent:.libspringloaded-1.2.5.RELEASE.jar-noverify
Spring Boot热部署-Devtools的使用
- Spring Loader与Devtools的区别是什么?
SpringLoader是真正意义上的热部署,而Devtools只能算是热启动
SpringLoader:SpringLoader 在部署项目时使用的是热部署的方式。 DevTools:DevTools 在部署项目时使用的是重新部署的方式
- 使用Devtools实现热部署的步骤是什么?
1.添加依赖:

2.启动项目