前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC_总结_03_SpringMVC相关注解

SpringMVC_总结_03_SpringMVC相关注解

作者头像
shirayner
发布2018-09-20 10:55:19
3370
发布2018-09-20 10:55:19
举报
文章被收录于专栏:Java成神之路Java成神之路

一、前言

在前面的小节中,我们配置了注解驱动和自动扫描包,然后就可以使用SpringMVC相关注解了。

二、@Controller

@Controller用来修饰类,源码如下:

代码语言:javascript
复制
package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    String value() default "";
}

(1)被@Component修饰,说明此注解用来标识一个bean。

(2)用来说明被修饰的类为控制器类。

三、@RequestMapping

1.属性详解

@RequestMapping注解用来修饰类或方法,源码如下:

代码语言:javascript
复制
package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String[] value() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

(1) value

value属性用将请求URL(如"/hello") 映射到整个类或特定的处理方法上。

value属性是RequestMapping的默认属性,也就是说  @RequestMapping( value = "/hello")   可以写成 @RequestMapping("/hello") 。

(2)method 

method 属性用来指定如下请求方式:GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.

2.注解简化

@RequestMapping(value= "/hello",Method=RequestMethod.GET ) 可简写成 @GetMapping("/hello")

类似注解还有:

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

四、参数Bean

Spring 会为如下类型的参数自动注入其bean对象。

见:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments

五、方法参数

可用如下注解用来修饰请求参数。

1.@RequestParam

可通过RequestParam来接收请求参数

代码语言:javascript
复制
@Controller
@RequestMapping("/pets")
public class EditPetForm {

    // ...

    @GetMapping
    public String setupForm(@RequestParam("petId") int petId, Model model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute("pet", pet);
        return "petForm";
    }

    // ...

}

2.@RequestHeader

通过此注解可以获取请求头相关信息。

例如,一个请求头为:

代码语言:javascript
复制
Host                    localhost:8080
Accept                  text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language         fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding         gzip,deflate
Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive              300

则可以通过如下方式获取请求头信息

代码语言:javascript
复制
@GetMapping("/demo")
public void handle(
        @RequestHeader("Accept-Encoding") String encoding,
        @RequestHeader("Keep-Alive") long keepAlive) {
    //...
}

3.@CookieValue

获取cookie

代码语言:javascript
复制
@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) {
    //...
}

4.@PathVariable 

可通过@PathVariable 来接收路径变量

代码语言:javascript
复制
@Controller
@RequestMapping("/owners/{ownerId}")
public class OwnerController {

    @GetMapping("/pets/{petId}")
    public Pet findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
        // ...
    }
}

5.@ModelAttribute

标注在方法参数上的@ModelAttribute说明了该方法参数的值将由model中取得。如果model中找不到,那么该参数会先被实例化,然后被添加到model中。在model中存在以后,请求中所有名称匹配的参数都会填充到该参数中。

代码语言:javascript
复制
@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
public String processSubmit(@ModelAttribute Pet pet) { }

以上面的代码为例,这个Pet类型的实例可能来自哪里呢?有几种可能:

  • 它可能因为@SessionAttributes标注的使用已经存在于model中
  • 它可能因为在同个控制器中使用了@ModelAttribute方法已经存在于model中——正如上一小节所叙述的
  • 它可能是由URI模板变量和类型转换中取得的(下面会详细讲解)
  • 它可能是调用了自身的默认构造器被实例化出来的
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、@Controller
  • 三、@RequestMapping
    • 1.属性详解
      • 2.注解简化
      • 四、参数Bean
      • 五、方法参数
        • 1.@RequestParam
          • 2.@RequestHeader
            • 3.@CookieValue
              • 4.@PathVariable 
                • 5.@ModelAttribute
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档