zoukankan      html  css  js  c++  java
  • spring mvc学习笔记二:@RequestMapping

    @RequestMapping

    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

    @RequestMapping注解有六个属性:value、method、params、header、 consumes,produces

    1、value:指定请求的实际地址 。

    2、method:指定请求的method类型, GET、POST、PUT、DELETE等。

    3、params:指定request中必须包含指定的参数值,才让该方法处理。

    4、headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。 

    value和method是常用属性,params和headers属性了解即可。

    案例:

    1、value/method

    value值:

    ①value的值为普通值,如value="helloworld"

    ②value的值含变量,如value="hellowrold/{id}"  此处使用了{}占位符来传递变量,可以使用@PathVariable注解取值,后面会有例子

    当然value的值还可以为Ant风格或者是正则表达式,由于不是很常用,此处略过。

    案例1:value值为普通值,method为GET请求方式

    index.jsp

    <a href="helloWorld">点击我跳转到hello.js页面</a>
     
     
    HelloWorldController.java
     
    package com.springmvc;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
     
    @Controller
    public class HelloWorldController {
     
    /**
    * 1. 使用 @RequestMapping 注解来映射请求的 URL
    * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
    * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
    *
    * /WEB-INF/views/hello.jsp
    *
    * @return
    */
    @RequestMapping(value="helloWorld",method=RequestMethod.GET)
    public String helloWorld(){
    System.out.println("Hello World");
    return "hello";
    }
    }
     
     

    案例2:value的值含变量,method为GET请求方式

    index.jsp

    <a href="requestMapping/Are you ok">传递参数</a>

    HelloWorldController.java

     1 package com.springmvc;
     2 import org.springframework.stereotype.Controller;
     3 import org.springframework.web.bind.annotation.PathVariable;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestMethod;
     6 @Controller
     7 public class HelloWorldController {
     8     
     9         
    10     @RequestMapping(value="/requestMapping/{info}",method=RequestMethod.GET)
    11     public String testRequestMapping(@PathVariable(value="info") String info){
    12         System.out.println("info:"+info);
    13         return "hello";
    14     }
    15 }
     
    这个例子中用到了@PathVariable注解,该注解可以将URL中的占位符映射到目标方法的参数中。
     

    案例3:value的值为普通值,method为POST请求方式

    index.jsp

    <form action="springmvc/testPostMethod" method="POST">
      <input type="submit" value="post请求" />
    </form>
     
    SpringMVCTest.java
     1 package com.springmvc;
     2 import org.springframework.stereotype.Controller;
     3 import org.springframework.web.bind.annotation.PathVariable;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestMethod;
     6 @RequestMapping("/springmvc")
     7 @Controller
     8 public class SpringMVCTest {
     9     private static String SUCCESS = "success";
    10     
    11     @RequestMapping(value="testPostMethod",method=RequestMethod.POST)
    12     public String testPostMethod(){
    13         System.out.println("POST Request success");
    14         return SUCCESS;
    15     }    
    16     
    17 }

     
    2、params/headers
     
    params和headers可以更加精确的映射请求,支持简单的表达式。
     
    SpringMVCTest.java
     
     1 package com.springmvc;
     2 import org.springframework.stereotype.Controller;
     3 import org.springframework.web.bind.annotation.PathVariable;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestMethod;
     6 @RequestMapping("/springmvc")
     7 @Controller
     8 public class SpringMVCTest {
     9     private static String SUCCESS = "success";
    10     
    11     /**
    12      * 该方法处理同时满足以下条件
    13      *1、 params中包含username参数并且age=10
    14      *2、headers中Accept-Language参数值为zh-CN,zh;q=0.8
    15      *的请求
    16      *
    17      * @return
    18      */
    19     @RequestMapping(value="/testParamsAndHeader",params={"username","age=10"},headers={"Accept-Language=zh-CN,zh;q=0.8"})
    20     public String testParamsAndHeader(){
    21         System.out.println("Test Params and Header");
    22         return SUCCESS;
    23     }
    24     
    25     /**
    26      * 该方法处理params中包含username参数并且age=10的请求
    27      * @return
    28      */
    29     @RequestMapping(value="/testParams",params={"username","age=10"})
    30     public String testParams(){
    31         System.out.println("Test Params");
    32         return SUCCESS;
    33     }
    34     
    35     
    36     
    37 }

     
     
     
     
     
  • 相关阅读:
    POJ-1189 钉子和小球(动态规划)
    POJ-1191-棋盘分割(动态规划)
    Java实现 LeetCode 730 统计不同回文子字符串(动态规划)
    Java实现 LeetCode 730 统计不同回文子字符串(动态规划)
    Java实现 LeetCode 729 我的日程安排表 I(二叉树)
    Java实现 LeetCode 729 我的日程安排表 I(二叉树)
    Java实现 LeetCode 729 我的日程安排表 I(二叉树)
    Java实现 LeetCode 728 自除数(暴力)
    Java实现 LeetCode 728 自除数(暴力)
    Java实现 LeetCode 728 自除数(暴力)
  • 原文地址:https://www.cnblogs.com/zuidongfeng/p/5699576.html
Copyright © 2011-2022 走看看