springboot教程-第一个controller

概述

springboot框架中的 Controller,相当于增强的 servlet:

  1. 接收请求(get、post、ajax)
  2. 获取请求中的参数(url中的参数,post表单参数,ajax参数、上传文件)
  3. 做出响应。(转发页面、重定向页面、返回json)

用 Controller 代替 Servlet,不需要写 Servlet

优点:

更简单的代码,就可以实现更多的功能

创建 controller 类

package top.malaoshi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//说明该类是Controller
@Controller
public class HelloCtrl {

    //映射路径,也就是说浏览器访问 `http://ip地址:port端口号/context上下文/hello` 时,就能够进入该方法
    @RequestMapping("/hello")
    public String hello(){
        //返回值是字符串时,表示页面名字,转发到hi.html,注意:该页面必须在静态资源目录下
        return "hi.html";
    }
}

创建 hi.html 页面

在静态资源目录下,创建 hi.html 文件,如下图位置:

内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello jsp
</body>
</html>

运行主启动类

测试

访问: http://localhost:8080/hello ,就进入到 HelloCtrlhello() 方法,该方法返回 hi.html 字符串,最终 转发classpath:/static/hi.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值