Spring MVC之Controller层的常用注解

Spring MVC之Controller层的常用注解

如果介绍有误请评论。

@Controller

通常写在类上

​ 作用:处理页面的HTTP请求

@RestController

通常写在类上

相当于@Controller +@ResponseBody@ResponseBody作用见下文

@RequestMapping

作用:既可以作用在类上,也可以作用在方法上,也可以同时作用在类和方法上,其作用是指明该类或者该方法响应哪个路径下的请求

通常使用这些注解都是Controller后缀加配置Mapping后缀来使用的。以下是**@Controoller+@RequestMapping**的使用。

package com.it.lms.controller;

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

/**
 * HelloController控制器类——返回一个String字符串
 * @author CZX
 */
@Controller
@RequestMapping("/")
public class HelloController {

    @RequestMapping(value = {"/login","/","/login.html"})
    public String hello(){
        //调用业务,接受前端参数
        return "login";
    }

    @RequestMapping(value = "/index")
    public String toIndex(){
        return "index";
    }
}

第一个@Controller@RequestMapping("/"),在启动项目之后,访问http://localhost:8080/的页面请求,就会调用hello()方法来处理这个页面请求,这时页面就会显示login所对应的页面。 同理,访问http://localhost:8080/loginhttp://localhost:8080/login.html的页面请求亦是调用hello()方法来处理这个页面请求。

@RequestMapping(value = "/index"),则是你访问http://localhost:8080/index的页面请求,就会调用toIndex()方法来处理这个页面请求,这时页面就会显示index所对应的页面。

注意:本项目的默认端口为8080,访问地址默认为本机,使用@RequestMapping的时候,如果参数仅有value的话,可以省略不写。

使用这些需要先了解请求与处理方法之间的映射关系

页面请求通常是@RequestMapping("/")这些带有Mapping字眼的注解。例如hello()方法所对应的页面请求就有三个。


@ResponseBody

返回json字符串数据

@RequestMapping("/id")
    @ResponseBody
    public HashMap<String,String> toList(){
        HashMap<String,String> lists;
        lists = new HashMap<>();
        lists.put("code","1232");
        lists.put("status","1");
        return lists;
    }

访问http://localhost:8080/id,会看到如下页面

在这里插入图片描述

@GetMapping和@PostMapping的区别

@GetMapping是一个组合注解,是**@RequestMapping(method = RequestMethod.GET)**的缩写

@PostMapping也是一个组合注解,是**@RequestMapping(method = RequestMethod.POST)**的缩写

	@RequestMapping(value = "/register")
    public String toRegister(){
        return "register";
    }
    @RequestMapping(value = "/register1")
    public String toRegister1(){
        return "register1";
    }
    @GetMapping("toRegister1")
    public String Register1( String username,
                        String password){

        return "index";
    }

    @PostMapping("toRegister")
    public String register(@RequestParam String username,
                           @RequestParam String password){

        return "index";
    }

在浏览器分别访问http://localhost:8080/register1http://localhost:8080/register输入123,33,点击注册跳转到的页面的url分别是

http://localhost:8080/toRegister1?username=123&password=33http://localhost:8080/toRegister

这两个注解的不同就是提交方式的不同,从而导致跳转的url不同

项目源码地址:gitee项目源码
资源链接:项目源码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

czxboys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值