spring boot 注解

一级注解:(写在类名前面的)
@RestController
: 等价于在函数前面写@ResponseBody ,会直接返回要显示的内容

@Controller
String返回的是模板文件的名称。

二级注解(写在类中的函数旁边的)
@RequestMapping("/web")
响应浏览器的地址 uri/web

示例:

1 @RestController
2 public class HelloWorldController {
3   @RequestMapping("/hello")
4   public String hello(){    
6     return "hello world~";
7   }
8 }

或者

1 @Controller
2 public class HelloWorldController {
3    @RequestMapping("/hello")
4     @ResponseBody
5    public String hello(){    
6      return "hello world~";
7    }

自动注入service,在controller处(类内部第一行)加入@Autowired,在对应的service class的类名前加入@Service

@RequestMapping(value="User",method=RequestMethod.POST)

写在函数的参数括号里的
@RequestMapping(value="User",method=RequestMethod.POST)
public String addUser(@ModelAttribute User user)
{
}

前端标签
<h1 th:text="${msg}"> hello spring boot</h1>
th:text="${msg}" 其中 th代表theameleaf的缩写 text表示替换的是文本 “${}”是格式 msg 是后台
传入的ModelMap格式的值里的一个属性

ModelMap是spring的内置对象,用于向页面传递数据,属性放在这个对象里,会自动传给前端页面

原文地址:https://www.cnblogs.com/xiangyu5945/p/9435478.html