论坛项目写的hello controller

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>

在有道笔记写了,可以看那个。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值