zoukankan      html  css  js  c++  java
  • SpringMVC_总结_03_SpringMVC相关注解

    一、前言

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

    二、@Controller

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

    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 "";
    }
    View Code

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

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

    三、@RequestMapping

    1.属性详解

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

    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 {};
    }
    View Code

    (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来接收请求参数

    @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";
        }
    
        // ...
    
    }
    View Code


    2.@RequestHeader

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

    例如,一个请求头为:

    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

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

    @GetMapping("/demo")
    public void handle(
            @RequestHeader("Accept-Encoding") String encoding,
            @RequestHeader("Keep-Alive") long keepAlive) {
        //...
    }
    View Code

    3.@CookieValue

    获取cookie

    @GetMapping("/demo")
    public void handle(@CookieValue("JSESSIONID") String cookie) {
        //...
    }
    View Code

    4.@PathVariable 

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

    @Controller
    @RequestMapping("/owners/{ownerId}")
    public class OwnerController {
    
        @GetMapping("/pets/{petId}")
        public Pet findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
            // ...
        }
    }
    View Code

    5.@ModelAttribute

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

    @PostMapping("/owners/{ownerId}/pets/{petId}/edit")
    public String processSubmit(@ModelAttribute Pet pet) { }
    View Code

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

    • 它可能因为@SessionAttributes标注的使用已经存在于model中
    • 它可能因为在同个控制器中使用了@ModelAttribute方法已经存在于model中——正如上一小节所叙述的
    • 它可能是由URI模板变量和类型转换中取得的(下面会详细讲解)
    • 它可能是调用了自身的默认构造器被实例化出来的
  • 相关阅读:
    进程和线程
    分治算法
    MySQL-IN和Exists区别
    Java-悲观锁和乐观锁
    Spring如何解析Dubbo标签
    Java平台标准版本
    java常用的框架
    状态码
    算法
    java.c++.c#.c的区别
  • 原文地址:https://www.cnblogs.com/shirui/p/9575707.html
Copyright © 2011-2022 走看看