zoukankan      html  css  js  c++  java
  • SpringMVC学习 -- 使用 @RequestMapping 映射请求

    在控制器的类定义及方法出定义出都可以标注 @RequestMapping:

    • 类定义处:提供初步的请求映射信息。相对于 Web 应用的根目录。
    • 方法定义出:提供进一步的细分映射信息。相对于类定义处的 URL。若类定义处未标注 @RequestMapping , 则方法定义处标记的 URL 相对于 Web 应用的根目录。

    DispatcherServlet 截获请求后 , 就通过控制器上 @RequestMapping 提供的映射信息确定请求所对应的处理方法。

    web.xml 

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5          version="3.1">
     6 
     7     <context-param>
     8         <param-name>contextConfigLocation</param-name>
     9         <param-value>/WEB-INF/applicationContext.xml</param-value>
    10     </context-param>
    11     <listener>
    12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    13     </listener>
    14     <servlet>
    15         <servlet-name>dispatcher</servlet-name>
    16         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    17         <load-on-startup>1</load-on-startup>
    18     </servlet>
    19     <servlet-mapping>
    20         <servlet-name>dispatcher</servlet-name>
    21         <url-pattern>/</url-pattern>
    22     </servlet-mapping>
    23 </web-app>

    dispatcher-servlet.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     6     <!-- 扫描自定义包 -->
     7     <context:component-scan base-package="com.itdoc.springmvc"/>
     8 
     9     <!-- 配置试图解析器: 把 Controller 返回值解析成实际的物理视图 -->
    10     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    11         <property name="prefix" value="/WEB-INF/views/"/>
    12         <property name="suffix" value=".jsp"/>
    13     </bean>
    14 </beans>

    TestRequestMapping.java

     1 package com.itdoc.springmvc;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestMethod;
     7 
     8 /**
     9  * @BLOG http://www.cnblogs.com/goodcheap
    10  * @DESCRIBE RequestMapping 测试
    11  * @AUTHOR WángChéngDá
    12  * @DATE 2017-03-08 14:30
    13  */
    14 @Controller
    15 @RequestMapping("/springmvc")
    16 public class TestRequestMapping {
    17 
    18     private final static String SUCCESS = "success";
    19 
    20     /**
    21      * 1.@RequestMapping 除了修饰方法还可以修饰类。
    22      * 2.修饰类和修饰方法
    23      * 1) 修饰类: 提供初步的请求映射信息, 相对于 WEB 应用的根目录。
    24      * 2) 修饰方法: 提供进一步细化的请求映射信息, 相对于修饰类处的 URL。
    25      * 若修饰类处未标注 @RequestMapping, 则修饰方法处的 URL 相对于 WEB 应用的根目录。
    26      */
    27     @RequestMapping("/testreqmap")
    28     public String testReqMap() {
    29         System.out.println("I am TestRequestMapping's testReqMap method...");
    30         return SUCCESS;
    31     }
    32 }

    映射请求参数、请求方式或请求头

    @RequestMapping 的 value , method , params 及 headers 分别表示请求 URL , 请求方式 , 请求参数及请求头的映射条件 , 联合使用多个条件可让请求映射更加精确化。

    method 请求方式常用有四种:

    • method = RequestMethod.POST
    • method = RequestMethod.GET
    • method = RequestMethod.PUT
    • method = RequestMethod.DELETE
     1 package com.itdoc.springmvc;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestMethod;
     7 
     8 /**
     9  * @BLOG http://www.cnblogs.com/goodcheap
    10  * @DESCRIBE RequestMapping 测试
    11  * @AUTHOR WángChéngDá
    12  * @DATE 2017-03-08 14:30
    13  */
    14 @Controller
    15 @RequestMapping("/springmvc")
    16 public class TestRequestMapping {
    17 
    18     private final static String SUCCESS = "success";
    19 
    20     /**
    21      * 使用 method 属性来制定请求方式。
    22      *
    23      * @return
    24      */
    25     @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    26     public String testMethod() {
    27         System.out.println("I am TestRequestMapping's testMethod method...");
    28         return SUCCESS;
    29     }
    30 }

    params 和 headers 支持简单的表达式:

    •  param:表示请求必须包含名为 param 的请求参数。
    • !param:表示请求不能包含名为 param 的请求参数。
    • param != value: 表示请求包含名为 param 的请求参数 , 但其值不能为 value。
    • params 可以有多个参数 , 用逗号隔开。示例:params = {"username", "age!=20"}
     1 package com.itdoc.springmvc;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestMethod;
     7 
     8 /**
     9  * @BLOG http://www.cnblogs.com/goodcheap
    10  * @DESCRIBE RequestMapping 测试
    11  * @AUTHOR WángChéngDá
    12  * @DATE 2017-03-08 14:30
    13  */
    14 @Controller
    15 @RequestMapping("/springmvc")
    16 public class TestRequestMapping {
    17 
    18     private final static String SUCCESS = "success";
    19 
    20     /**
    21      * 可以使用 params 和 headers 来更加精确的映射请求, params 和 headers 支持简单的表达式。
    22      * @return
    23      */
    24     @RequestMapping(value = "testParamsAndHeaders", params = {"username", "age!=20"},
    25             headers = {"Accept-Language=zh-CN,zh;q=0.8"})
    26     public String testParamsAndHeaders() {
    27         System.out.println("I am TestRequestMapping's testParamsAndHeaders method...");
    28         return SUCCESS;
    29     }
    30 }

    Ant 风格资源地址支持3种匹配符:

    • ?:匹配文件名中的一个字符。
    • *:匹配文件名中的任意字符。
    • **:匹配多层路径。
     1 package com.itdoc.springmvc;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestMethod;
     7 
     8 /**
     9  * @BLOG http://www.cnblogs.com/goodcheap
    10  * @DESCRIBE RequestMapping 测试
    11  * @AUTHOR WángChéngDá
    12  * @DATE 2017-03-08 14:30
    13  */
    14 @Controller
    15 @RequestMapping("/springmvc")
    16 public class TestRequestMapping {
    17 
    18     private final static String SUCCESS = "success";
    19 
    20     @RequestMapping("/testAndPath/*/abc")
    21     public String testAndPath() {
    22         System.out.println("I am TestRequestMapping's testAntPath method...");
    23         return SUCCESS;
    24     }
    25 }

     

    @PathVariable 映射 URL 绑定占位符:

    • 带占位符的 URL 是 Spring3.0 新增的功能 , 该功能在 SpringMVC 的 REST 目标挺进发展过程中具有里程碑的意义。
    • 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的{xxx} 占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
    • 注意:@PathVariable("xxx") 中的 xxx 必须与占位符 {xxx} 中的 xxx 相同。 
     1 package com.itdoc.springmvc;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RequestMethod;
     7 
     8 /**
     9  * @BLOG http://www.cnblogs.com/goodcheap
    10  * @DESCRIBE RequestMapping 测试
    11  * @AUTHOR WángChéngDá
    12  * @DATE 2017-03-08 14:30
    13  */
    14 @Controller
    15 @RequestMapping("/springmvc")
    16 public class TestRequestMapping {
    17 
    18     private final static String SUCCESS = "success";
    19 
    20     /**
    21      * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中。
    22      * @param id
    23      * @return
    24      */
    25     @RequestMapping("/testPathVariable/{id}")
    26     public String testPathVariable(@PathVariable("id") Integer id) {
    27         System.out.println("I am TestRequestMapping's testPathVariable method...	 id=" + id);
    28         return SUCCESS;
    29     }
    30 }
  • 相关阅读:
    Mac下好玩的终端命令
    【bzoj3441】乌鸦喝水
    LeetCode[39]: 组合总和
    打击盗版,支持原创
    状态模式(State)-设计模式
    webpack核心概念
    吴裕雄--天生自然TensorFlow高层封装:Estimator-自定义模型
    吴裕雄--天生自然TensorFlow高层封装:Estimator-DNNClassifier
    吴裕雄--天生自然TensorFlow高层封装:Keras-TensorFlow API
    吴裕雄--天生自然TensorFlow高层封装:Keras-多输入输出
  • 原文地址:https://www.cnblogs.com/chinda/p/6523947.html
Copyright © 2011-2022 走看看