javascript验证用户名不能为空_《SpringBoot后端验证+异常处理+Junit单元测试整合+热部署》...

SpringBoot服务端数据-实现添加用户功能

    1. 创建一个Maven的jar工程。

7d796ec96443152161869037edd95ea9.png
    1. 修改POM文件添加Web启动器与Thymeleaf坐标。

771fc72182037a003cf848bf2562272d.png
    1. 在项目中使用Thymeleaf编写一个添加用户的视图。
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <form th:action="@{/addUser}" method="post">
  9. <p>姓名:<input type="text" name="name" th:field="${user.name}"><font color="red" th:errors="${user.name}"></font></p>
  10. <p>密码:<input type="text" name="password" th:field="${user.password}"><font color="red" th:errors="${user.password}"></font></p>
  11. <p>年龄:<input type="text" name="age" th:field="${user.age}"><font color="red" th:errors="${user.age}"></font></p>
  12. <p>邮箱:<input type="text" name="email" th:field="${user.email}"><font color="red" th:errors="${user.email}"></font></p>
  13. <p>性别:<select name="sex">
  14. <option value="男"></option>
  15. <option value="女"></option>
  16. </select><font color="red" th:errors="${user.sex}"></font>
  17. </p>
  18. <input type="submit" value="提交"><span th:text="${#httpServletRequest.getAttribute('msg')}"></span>
  19. </form>
  20. </body>
  21. </html>
    1. 创建一个Controller处理添加用户请求。
  1. package com.bjsxt.controller;
  2. import com.bjsxt.pojo.User;
  3. import com.bjsxt.service.OperateService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.validation.BindingResult;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import javax.validation.Valid;
  10. import java.util.List;
  11. @Controller
  12. public class FuncationController {
  13. @Autowired
  14. private OperateService service;
  15. @RequestMapping("/findAll")
  16. public String findAll(Model model){
  17. List<User> list = service.findAll();
  18. model.addAttribute("users", list);
  19. System.out.println(list);
  20. return "index";
  21. }
  22. /**
  23. * @Valid:开启对pojo实体类的数据验证
  24. * BindingResult:封装了校验的结果
  25. * @param user
  26. * @return
  27. */
  28. @RequestMapping("/addUser")
  29. public String addUser(@Valid User user,
  30. BindingResult result) {
  31. /**
  32. * result.hasErrors():判断数据校验是否通过,通过返回false,否则返回true
  33. * The method be used to judge that if the data validate is approved,return false or true
  34. */
  35. if (result.hasErrors()) {
  36. System.out.println("验证");
  37. return "adduser";
  38. }
  39. int i = service.addUser(user);
  40. if(i==1){
  41. return "redirect:/findAll";
  42. }
  43. else {
  44. return "error";
  45. }
  46. }
  47. }
  1. SpringBoot服务端数据-数据校验
    1. Spring Boot中服务端数据校验技术的特点是什么?

SpringBoot中使用了Hibernate-validate校验框架

  1. 阐述Spring Boot中如何实现服务端数据校验?

1.在pojo实体类中添加校验规则的注解:

  1. package com.bjsxt.pojo;
  2. import lombok.Data;
  3. import org.hibernate.validator.constraints.Length;
  4. import javax.validation.constraints.*;
  5. @Data
  6. public class User {
  7. /**
  8. * 在实体类中添加校验规则
  9. * @NotBlank:非空校验(判断字符串是否为null或空串,去掉首尾的空格)
  10. * @NotEmpty:非空校验(判断字符串是否为null或空串,不会去掉首尾的空格)
  11. * @Length:指定字符串的长度,最小:min ,最大:max
  12. * @Min():指定数值类型最小值
  13. * @Max():指定数值类型最大值
  14. * @Email:验证邮箱格式
  15. * @NotNull():数值类型非空校验
  16. */
  17. private Integer id;
  18. @NotBlank(message = "用户名不能为空")
  19. private String name;
  20. @NotEmpty(message = "密码不能为空")
  21. @Length(min = 6,max = 12)
  22. private String password;
  23. @NotBlank(message = "性别不能为空")
  24. private String sex;
  25. @Min(value = 1,message = "年龄最小为1岁")
  26. @Max(value = 150,message = "年龄最大不能超过150岁")
  27. @NotNull(message = "年龄不能为空")
  28. private Integer age;
  29. @Email
  30. @NotBlank(message = "邮箱不能为空")
  31. private String email;
  32. }
  33. Controller类中开启数据验证
  34. /**
  35. * @Valid:开启对pojo实体类的数据验证
  36. * BindingResult:封装了校验的结果
  37. * @param user
  38. * @return
  39. */
  40. @RequestMapping("/addUser")
  41. public String addUser(@Valid User user,
  42. BindingResult result) {
  43. /**
  44. * result.hasErrors():判断数据校验是否通过,通过返回false,否则返回true
  45. * The method be used to judge that if the data validate is approved,return false or true
  46. */
  47. if (result.hasErrors()) {
  48. System.out.println("验证");
  49. return "adduser";
  50. }
  51. int i = service.addUser(user);
  52. if(i==1){
  53. return "redirect:/findAll";
  54. }
  55. else {
  56. return "error";
  57. }
  58. }
  1. @NotBlank注解的作用是什么?

非空校验(判断字符串是否为null或空串,去掉首尾的空格)

  1. @Valid注解的作用是什么?

@Valid:开启对pojo实体类的数据验证

  1. BindingResult的作用是什么?

BindingResult:封装了校验的结果

  1. SpringBoot服务端数据-解决异常
    1. 在服务端数据校验时会出现什么异常?

fcb98403908a2bb3a2f3019c4b640576.png
  1. 产生异常的原因是什么?

页面跳转时,调用页面跳转的方法中没有User对象

  1. 如何解决该异常?

在跳转页面的方法中注入一个对象,要求参数对象的变量名必须是对象的类名的全称首字母小写。

84d83adce4587ed5012f901889c278d7.png
  1. @ModelAttribute注解的作用是什么?

如果想为传递的对象更改名称,可以使用@ModelAttribute("aa")这表示当前传递的对象的key为aa。

663bd95c410b6e1bc5b7dc262e689e60.png
  1. SpringBoot服务端数据-其他校验规则
    1. @NotBlank: 注解的作用是什么?

非空校验(判断字符串是否为null或空串,去掉首尾的空格)

    1. @NotEmpty: 注解的作用是什么?

非空校验(判断字符串是否为null或空串,不会去掉首尾的空格)

    1. @Length: 注解的作用是什么?

指定字符串的长度,最小:min ,最大:max

    1. @Min: 注解的作用是什么?

指定数值类型最小值

    1. @Max: 注解的作用是什么?

指定数值类型最大值

    1. @Email:注解的作用是什么?

验证邮箱格式

  1. SpringBoot异常处理-自定义错误页面
    1. 在Spring Boot中一共提供了几种处理异常的方式?

5种

  1. 什么是自定义错误页面方式?

SpringBoot 默认的处理异常的机制: SpringBoot 默认的已经提供了一套处理异常的机制。 一旦程序中出现了异常 SpringBoot 会像/error 的 url 发送请求。在 springBoot 中提供了一个 叫 BasicExceptionController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常 信息。

如 果 我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再 src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error

新建一个Thymeleaf页面作为自定义错误页面,命名必须为error.html

  1. 自定义错误页面的命名上有何要求?

命名必须为error.html

  1. 自义定错误页面应该放到项目的什么位置?

应该放在resources/templates目录下

  1. SpringBoot异常处理-@ExceptionHandler
    1. @ExceptionHandler注解的作用是什么?

@ExceptionHandler():指定处理的异常类型,当产生这些异常时,自动调用该方法

  1. 使用@ExceptionHandler注解处理异常的步骤是什么?
  1. /**
  2. * 方式二:@ExceptionHandler()注解
  3. *
  4. * @ExceptionHandler(value = {java.lang.NullPointerException.class})
  5. * 指定处理的异常类型,当产生这些异常时,自动调用该方法
  6. *
  7. * ModelAndView:封装异常信息以及视图的指定Mo
  8. *
  9. * Exception e:会将产生的异常对象注入该方法
  10. *
  11. * 缺点:1.如果处理的异常类型很多时,代码量会很多,代码冗余
  12. * 2.只能处理当前Controller,不具备跨Controller的能力
  13. * @param e
  14. * @return
  15. */
  16. @ExceptionHandler(value = {java.lang.NullPointerException.class})
  17. public ModelAndView nullPointerException(Exception e){
  18. ModelAndView mv=new ModelAndView();
  19. //将异常信息注入
  20. mv.addObject("error", e.toString());
  21. //指定视图:
  22. mv.setViewName("error");
  23. return mv;
  24. }
  1. SpringBoot异常处理-@ControlleAdvice
    1. @ControllerAdvice注解的作用是什么?

@ControllerAdvice注解修饰的异常处理类可以处理全局的异常

  1. @ControllerAdvice+@ExceptionHandler注解处理异常有什么特点?

1.如果处理的异常类型很多时,代码量会很多,代码冗余

2.只能处理当前Controller,不具备跨Controller的能力

  1. SpringBoot异常处理-SimpleMappingExceptionResolver
    1. SimpleMappingExceptionResolver的作用是什么?

全局异常处理类,使用SimpleMappingExceptionResolver 做全局异常处理

  1. 阐述使用SimpleMappingExceptionResolver处理异常的方式是什么?
  1. package com.bjsxt.exception;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
  5. import java.util.Properties;
  6. /**
  7. * 异常处理方式四:全局异常处理+处理异常方法简化
  8. * @Configuration+@Bean注解+SimpleMappingExceptionResolver对象
  9. * 缺点:不能传递异常信息
  10. */
  11. @Configuration
  12. public class GlobalExceptionHandle4 {
  13. /**
  14. * 该方法必须要有返回值:返回值类型为 SimpleMappingExceptionResolver
  15. * @return
  16. */
  17. @Bean
  18. public SimpleMappingExceptionResolver handleException(){
  19. SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver();
  20. Properties mappings=new Properties();
  21. /**
  22. * Properties类:
  23. * 参数一(key):表示异常的类型(异常类型的全名)
  24. * 参数二(value):表示要跳转的视图名称
  25. */
  26. mappings.put("java.lang.NullPointerException","error");
  27. mappings.put("java.lang.ArithmeticException","error2");
  28. resolver.setExceptionMappings(mappings);
  29. return resolver;
  30. }
  31. }
  1. SpringBoot异常处理-自定义HandlerExceptionResolver
    1. HandlerExceptionResolver接口的作用是什么?

异常统一处理接口

  1. 阐述使用HandlerExceptionResolver接口处理异常的方式是什么?
  1. package com.bjsxt.exception;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.HandlerExceptionResolver;
  5. import org.springframework.web.servlet.ModelAndView;
  6. import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.util.Properties;
  10. /**
  11. * 异常处理方式五:全局异常处理+处理异常方法简化
  12. * @Configuration+实现HandlerExceptionResolver接口
  13. * 缺点:不能传递异常信息
  14. */
  15. @Configuration
  16. public class GlobalExceptionHandle5 implements HandlerExceptionResolver {
  17. @Override
  18. public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
  19. ModelAndView mv=new ModelAndView();
  20. if(e instanceof java.lang.NullPointerException){
  21. mv.setViewName("error");
  22. }else if(e instanceof java.lang.ArithmeticException){
  23. mv.setViewName("error2");
  24. }
  25. mv.addObject("error", e);
  26. return mv;
  27. }
  28. }
  1. Spring Boot整合junit单元测试
    1. @RunWith注解的作用是什么?

@RunWith():启动类,表示项目将从该类开始运行

  1. @SpringBootTest注解的作用是什么?

@SpringBootTest()表示当前类是SpringBoot的测试类,并加载SpringBoot的启动类

  1. Spring Boot热部署-SpringLoader-方式一
    1. 使用SpringLoader实现热部署的方式有几种?

两种

  1. SpringLoader实现热部署有什么缺陷?
  1. 该种方式只能对后端代码进行热部署,对前端代码无能为力
  2. 关闭需要在任务管理器结束进程
    1. 如何启动通过Maven插件方式引入SpringLoader实现热部署的服务?

59b74a3c95f3a271a89987eb60b94dbe.png
  1. 如何关闭使用SpringLoader热部署的服务?

需要在任务管理器中关闭

  1. Spring Boot热部署-SpringLoader-方式二
    1. 手动添加SpringLoader的jar实现项目的热部署的步骤是什么?

a2168cc8560cec2f0f9455c9b3d22116.png

f276b1dcac7cbe3ffc371efd01e49ad3.png

3f2b947b38646b4c6e5531e481d7ed9b.png
  1. 启动服务时与基于插件方式添加SpringLoader方式有何区别?

基于插件方式需要使用Maven命令:spring-boot:run

基于jar包方式:

3f2b947b38646b4c6e5531e481d7ed9b.png
  1. 启动服务时需要添加什么启动参数?

-javaagent:.libspringloaded-1.2.5.RELEASE.jar-noverify

Spring Boot热部署-Devtools的使用

  1. Spring Loader与Devtools的区别是什么?

SpringLoader是真正意义上的热部署,而Devtools只能算是热启动

SpringLoader:SpringLoader 在部署项目时使用的是热部署的方式。 DevTools:DevTools 在部署项目时使用的是重新部署的方式

  1. 使用Devtools实现热部署的步骤是什么?

1.添加依赖:

294967bb4bcb2d45c6597f44890c81d9.png

2.启动项目