package forum.kaoyan.qinghuagong.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
//注意首字母大写
//加注解后,就会自动识别扫描当前类,把它当做spring的一个并局管理,同时也识别它为controller,意思就是允许这个类
//接收前端的请求。
@Controller
public class IndexController {
@GetMapping("/hello")
public String hello(@RequestParam(name="name")String name, Model model){
model.addAttribute("name",name);
return "index";//写完这句再去templates下写hello.html
}
}
然后在resources/template下的index.html写
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org"> //加入xmlns:....以后,才能让spring知道我要解析模板时,它是我能支持的我一个模板,我的模板引擎需要解析它。
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" /> //th:text是spring里的一个动态语法,作用是把model的key为name的把${..}替换掉。
</body>
</html>
在有道笔记写了,可以看那个。