zoukankan      html  css  js  c++  java
  • (入门SpringBoot)SpringBoot后台验证(八)

      后台验证的作用主要是防止postman...等等工具的恶意提交,前后台都判断数据,双保险.

    1.可以在SpringBoot传参数 加上NotNull....
    //分组Default,分组的好处就是可重复使用.
    public interface Default{}
    //Update
    public interface Update{}
    /**
     * 编号.
     */
    @NotNull(message = "id不能为空",groups = Update.class)
    private Integer id;
    
    /**
     * 状态码
     */
    @NotBlank(message = "请输入状态码",groups = Default.class)
    private String type_code;
    /**
     * 状态的值
     */
    @NotBlank(message = "请输入状态值",groups = Default.class)
    private String type_key;
    /**
     * 状态值,前台展示的值
     */
    @NotBlank(message = "请输入字典表显示值",groups = Default.class)
    private String type_value;
    /**
     * 备注
     */
    private String remark;
    2.在类上这么写:
    @RequestMapping("/index")
    public String index(@RequestBody @Validated(value = Testform.Default.class) Testform wordbookform, BindingResult bindingResult){
        if(bindingResult.hasErrors()){//错误:
            return bindingResult.getFieldError().getDefaultMessage();
        }
       
        return "index";
    }
    
    @RequestMapping("/index02")
    public String index02(@RequestBody @Validated(value = Testform.Update.class) Testform wordbookform, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return bindingResult.getFieldError().getDefaultMessage();
        }
        return "index";
    }

        

  • 相关阅读:
    uva 10369 Arctic Network
    uvalive 5834 Genghis Khan The Conqueror
    uvalive 4848 Tour Belt
    uvalive 4960 Sensor Network
    codeforces 798c Mike And Gcd Problem
    codeforces 796c Bank Hacking
    codeforces 768c Jon Snow And His Favourite Number
    hdu 1114 Piggy-Bank
    poj 1276 Cash Machine
    bzoj 2423 最长公共子序列
  • 原文地址:https://www.cnblogs.com/historylyt/p/10933926.html
Copyright © 2011-2022 走看看