SpringMVC (二)测试@RequestMapping

上一篇 SpringMVC 的 HelloWorld 实例


Spring MVC 的 @RequestMapping 注解

  • @RequestMapping 既可以修饰,也可以修饰方法
    – 修饰 Class 类:提供初步映射信息
    – 修饰 Method 方法:提供细化的映射信息
    若类 Class 未标注 @RequestMapping,则方法处标记的 URL 相对于
    WEB 应用的根目录
  • @RequestMapping 的 value、method、params 及 heads
    分别表示请求 URL、请求方式(POST PUT)、请求参数及请求头的映射条
    件,他们之间是的关系,联合使用多个条件可让请求映射
    更加精确化。

测试 @RequestMapping 的 method

结构如下:


HelloWorld.java 如下(测试 method=RequestMethod.POST)):

package com.atguigu.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping(value = "/springmvc" ,method=RequestMethod.POST)
@Controller
public class HelloWorld {
    @RequestMapping(value = "/springmvc",method= RequestMethod.POST)
    public String hello() {
        System.out.println("hello world");
        return "success2";
    }
}

SpringMVC.xml(设置后台访问路径前缀、后缀)







    
    



web.xml



    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet

        
            contextConfigLocation
            classpath:SpringMVC.xml
        

        1
    

    
        SpringMVC
        /
    

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    index.jsp



success page.




views文件夹下的 success2.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    success page


    

success!

访问路径:http://localhost:9090/

结果:
点击 success page.



点击 submit


结论:
在 HelloWorld.java 类中写 @RequestMapping(value = "/springmvc" ,method=RequestMethod.POST),请求由默认的 get 变为 post


测试 @RequestMapping 的 params

对上述 HelloWorld.java 进行修改:

package com.atguigu.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(value = "/springmvc" ,params={"username","age!=10"})
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=en-US,zh;q=0.8"})
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"})
//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=zh-CN,zh;q=0.8"})
@Controller
public class HelloWorld {
    @RequestMapping(value = "/springmvc")
    public String hello() {
        System.out.println("hello world");
        return "success2";
    }
}
  • 结果1:
    访问成功:http://localhost:9090/springmvc/springmvc?username=isj&age=11
    访问失败:http://localhost:9090/springmvc/springmvc?username=isj&age=10因为age不能为10
    结论:@RequestMapping 的 params 参数表示访问中必须包含某个参数

测试 @RequestMapping 的 headers

package com.atguigu.springmvc.handlers;

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

//@RequestMapping(value = "/springmvc" ,params={"username","age!=10"})
@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=zh-CN,zh;q=0.9,zh-TW;q=0.8"})
@Controller
public class HelloWorld {
    @RequestMapping(value = "/springmvc")
    public String hello() {
        System.out.println("hello world");
        return "success2";
    }
}

对结果页面打开开发者工具(按F12)找到network标签下localhost文件选中,并刷新整个页面(为了得到headers),查看localhost可以得到request headers 如下

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Cookie: JSESSIONID=DCB16D54094CF3DBFB7B22482B9F0077
DNT: 1
Host: localhost:9090
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36

开发者工具如下:



以 Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8 为例对HelloWorld.java修改@requestMapping()的headers参数。

package com.atguigu.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=zh-CN,zh;q=0.9,zh-TW;q=0.8"})
@Controller
public class HelloWorld {
    @RequestMapping(value = "/springmvc")
    public String hello() {
        System.out.println("hello world");
        return "success2";
    }
}

访问成功:
http://localhost:9090/springmvc/springmvc?username=isj&age=11
访问失败:当 @RequestMapping(value = "/springmvc" ,params={"username","age!=10"},headers={"Accept-language=en-US,zh;q=0.9,zh-TW;q=0.8"}) 则无法访问http://localhost:9090/springmvc/springmvc?username=isj&age=11

结论: @RequestMapping 的 headers 表示请求头包含某个参数必须与浏览器对应才能访问。

@RequestMapping() 的 value 支持 ant 风格三种匹配符:

?:匹配文件名中的一个字符
*:匹配文件名中的任意多个字符
**:** 匹配多层路径

例如:下面的地址可以为 testAntPath/a/abc 或者 testAntPath/abc/abc

@RequestMapping("/testAntPath/*/abc")
public String testAntPath(){
  System.out.println("testAntPath")
}

Reference:感谢佟刚大神的SpringMVC视频教程

你可能感兴趣的:(SpringMVC (二)测试@RequestMapping)