iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Spring MVC的常用注解有哪些及怎么用
  • 928
分享到

Spring MVC的常用注解有哪些及怎么用

2024-04-02 19:04:59 928人浏览 薄情痞子
摘要

这篇文章主要介绍了spring mvc的常用注解有哪些及怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring MVC的常用注解有哪些及怎么用文章都会有所收获,下面我

这篇文章主要介绍了spring mvc的常用注解有哪些及怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring MVC的常用注解有哪些及怎么用文章都会有所收获,下面我们一起来看看吧。

Spring Boot 默认集成了Spring MVC,下面为Spring MVC一些常用注解。

开发环境:IntelliJ idea 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目

一、Controller注解

Controller注解用于修饰Java类,被修饰的类充当MVC中的控制器角色。
Controller注解使用了@Component修饰,使用Controller注解修饰的类,会被@ComponentScan检测,并且会作为Spring的bean被放到容器 
中。

package com.example.demo;

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

@Controller
public class DemoController {

    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return "index";
    }
}

运行项目后,浏览器访问:Http://localhost:8080/index,页面显示:
index

二、RestController注解

RestController注解是为了更方便使用@Controller和@ResponseBody。
@ResponseBody修饰控制器方法,方法的返回值将会被写到HTTP的响应体中,所返回的内容不放到模型中,也不会被解释为视图的名称。
下面例子等同于上面例子。

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

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

三、RequestMapping注解

RequestMapping注解可修饰类或方法,主要用于映射请求与处理方法。
当用于修饰类并设置了URL时,表示为各个请求设置了URL前缀。
RequestMapping注解主要有以下属性:
(1)path与value:用于配置映射的url;
(2)method:映射的HTTP方法,如GET、POST、PUT、DELETE;
也可以使用默认配置了@RequestMapping的method属性的几个注解:
@GetMapping等同于RequestMapping(method="RequestMethod.GET")
@PostMapping、@PutMapping、@DeleteMapping类似。
(3)params:为映射的请求配置参数标识;
(4)consumes:配置请求的数据类型,如XML或JSON等;
(5)produces:配置响应的数据类型,如“application/json”返回json数据;

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/oa")
public class DemoController {

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

    @RequestMapping(value = "/index2", method = RequestMethod.GET)
    public String index2(){
        return "index2";
    }

    @GetMapping(value = "/index3")
    public String index3(){
        return "index3";
    }
}

浏览器分别访问:
http://localhost:8080/oa/index1
http://localhost:8080/oa/index2
http://localhost:8080/oa/index3
页面分别显示:
index1
index2
index3

四、PathVariable注解

PathVariable注解主要用于修饰方法参数,表示该方法参数是请求URL的变量。

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/index1/{name}")
    public String index1(@PathVariable String name){
        return "index1: " + name;
    }

    //可以为@PathVariable配置属性值,显式绑定方法参数与URL变量的值
    @GetMapping("/index2/{name}")
    public String index2(@PathVariable("name") String lc){
        return "index2: " + lc;
    }
}

浏览器访问http://localhost:8080/index1/a
页面显示:
a
访问http://localhost:8080/index1/b
页面显示:
b

五、RequestParam注解

RequestParam注解用于获取请求体中的请求参数,如表单提交后获取页面控件name值。

package com.example.demo;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class DemoController {

    @PostMapping("/index1")
    public String index1(@RequestParam String userName){
        return userName;
    }

    //map存放所有请求参数
    @PostMapping("/index2")
    public String index2(@RequestParam Map<String,String> map){
        String age = map.get("age");
        String sex = map.get("sex");
        return age + "," + sex;
    }
}

随便在电脑中如桌面新建一个html文件:

<html>
<body>
  <fORM method="post" action="http://localhost:8080/index1">
    <input type="text" name="userName" value="abc" />    
    <input type="submit" value="提交1" />
  </form>
  <form method="post" action="http://localhost:8080/index2">
    <input type="text" name="age" value="22" />
    <input type="passWord" name="sex" value="male" />    
    <input type="submit" value="提交2" />
  </form>
</body>
</html>

浏览器打开后,如果点击“提交1”按钮后,页面跳到http://localhost:8080/index1,显示abc。
如果点击“提交2”按钮后,页面跳到http://localhost:8080/index2,显示22,male。

六、文件上传

使用RequestParam注解可以实现文件上传。

package com.example.demo;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class DemoController {

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String filePath = "D:/";
        File dest = new File(filePath + fileName);
        file.transferTo(dest);
        return "上传成功";
    }

}

随便新建一个html文件

<html>
<body>
  <form method="post" action="http://localhost:8080/upload" enctype="multipart/form-data">
    <input type="file" name="file" />    
    <input type="submit" value="提交" />
  </form>  
</body>
</html>

浏览器打开后,选择一个文件,点击提交后,文件保存到了D盘。

关于“Spring MVC的常用注解有哪些及怎么用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Spring MVC的常用注解有哪些及怎么用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网JavaScript频道。

--结束END--

本文标题: Spring MVC的常用注解有哪些及怎么用

本文链接: https://www.lsjlt.com/news/93623.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring MVC的常用注解有哪些及怎么用
    这篇文章主要介绍了Spring MVC的常用注解有哪些及怎么用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring MVC的常用注解有哪些及怎么用文章都会有所收获,下面我...
    99+
    2024-04-02
  • spring mvc中的注解有哪些
    这篇文章给大家介绍spring mvc中的注解有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1. @ControllerController控制器是通过服务接口定义的提供访问应用程序的一种行为,它解释用户的输入,...
    99+
    2023-05-31
    springmvc
  • Spring注解有哪些及怎么使用
    这篇文章主要介绍了Spring注解有哪些及怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring注解有哪些及怎么使用文章都会有所收获,下面我们一起来看看吧。Spring原始注解Spring是轻代码而...
    99+
    2023-07-02
  • spring boot中有哪些常用的注解
    spring boot中有哪些常用的注解?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。@RestController和@RequestMapping注解0重要的一个新的改进...
    99+
    2023-05-31
    springboot 中有 注解
  • Rails、MVC及最常用的Rails命令有哪些
    本篇内容主要讲解“Rails、MVC及最常用的Rails命令有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Rails、MVC及最常用的Rails命令有哪些...
    99+
    2024-04-02
  • Spring Boot Rest常用框架注解有哪些
    本篇内容主要讲解“Spring Boot Rest常用框架注解有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Boot Rest常用框架...
    99+
    2023-07-02
  • Spring常用的注入方式有哪些
    Spring常用的注入方式有以下几种:1. 构造器注入(Constructor Injection):通过构造器来注入依赖对象。2....
    99+
    2023-08-15
    Spring
  • Spring IOC常用注解有哪些与如何使用
    本文小编为大家详细介绍“Spring IOC常用注解有哪些与如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring IOC常用注解有哪些与如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来...
    99+
    2023-06-30
  • SpringMVC常用的注解有哪些
    SpringMVC常用的注解有:1. @Controller:用于标识一个类是SpringMVC的控制器。2. @RequestMa...
    99+
    2023-09-13
    SpringMVC
  • 常用的Springboot注解有哪些
    常用的Spring Boot注解有以下几种:1. @SpringBootApplication:该注解用于标记启动类,表示该类是Sp...
    99+
    2023-10-11
    springboot
  • php常用mvc框架有哪些
    php中常用的mvc框架有PHPDevShell、ZooP、Yii、SymfonyPHPDevShellPHPDevShell是一款开源的快速应用mvc开发框架,其目标在于开发插件一类的基于管理的应用,常用于开发不包含Javascript的...
    99+
    2024-04-02
  • java常用注解有哪些
    Java常用注解包括:1. @Override:用于标注方法覆盖父类方法。2. @Deprecated:用于标注不推荐使用的方法或类...
    99+
    2023-05-29
    java常用注解 java
  • Spring Data JPA 实体类中常用注解有哪些
    本篇内容主要讲解“Spring Data JPA 实体类中常用注解有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring Data J...
    99+
    2023-06-25
  • hibernate常用注解有哪些
    Hibernate常用注解有:1. @Entity:指示该类是一个实体类,对应数据库中的一个表。2. @Table:指定实体类对应的...
    99+
    2023-09-16
    hibernate
  • SpringBoot常用注解有哪些
    这篇文章给大家分享的是有关SpringBoot常用注解有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。  SpringBoot注解  @SpringBootApplication:开启Spring Boot自...
    99+
    2023-06-02
  • mybatis常用注解有哪些
    MyBatis常用的注解有以下几种:1. @Mapper:用于标识该接口是一个MyBatis的Mapper接口。2. @Select...
    99+
    2023-09-16
    mybatis
  • Spring MVC的完整执行流程和常用组件有哪些
    这篇文章主要介绍“Spring MVC的完整执行流程和常用组件有哪些”,在日常操作中,相信很多人在Spring MVC的完整执行流程和常用组件有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家...
    99+
    2023-07-02
  • SpringBoot中常用的注解有哪些
    这篇文章主要介绍了SpringBoot中常用的注解有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、@SpringBootApplication此注解是Spring B...
    99+
    2023-06-21
  • SpringBoot中有哪些常用的注解
    这篇文章将为大家详细讲解有关SpringBoot中有哪些常用的注解,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。@SpringBootApplication...
    99+
    2024-04-02
  • SpringMVC中有哪些常用注解
    SpringMVC中有哪些常用注解?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。MVC简介MVC 全名是 Model View Controller,是模型(model)-视图...
    99+
    2023-06-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作