一些关于springboot的零碎知识点

1:在application.yaml中为类元素注入值

(@value一个一个注解也可以)

这里通过在Persion类中添加ConfiguretionProperties(prefix = ‘person’)
prefix = ‘person’这个地方对应了application.yaml文件

也可以使用springEL表达式注入值(${…}):一些关于springboot的零碎知识点_第1张图片

2:

松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定

3:JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性

比如邮箱: @Emali

3:springboot的多环境配置,可以选择激活哪一个配置文件

一些关于springboot的零碎知识点_第2张图片
.yml简写

一些关于springboot的零碎知识点_第3张图片

4:为什么springboot中controller返回不了templates中的html文件?

注意的是用@controller注解不要用@restController,前者是渲染页面用的,后者是返回数据用的

5:重定向界面:

package com.example.springbootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 14:00
 */
@Controller
public class LoginController {

    @RequestMapping("/user/login")
    public  String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            Model model){

        //具体业务
        if (!StringUtils.isEmpty(username) && "123456".equals(password)){
            return "redirect:/main.html";
        }
        //告诉用户登陆失败了
        else {
            model.addAttribute("msg","用户名或密码错误");
            return "index";

        }

    }
}


你可能感兴趣的:(Springboot学习,spring,boot,spring,java)