@RequestMapping相关属性

134 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

value属性

用来设置请求路径;值是一个字符串数组;可以设置多个路径共同访问对应方法

示例:

1. index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>index</title>
</head>
<body>
      <a href="${pageContext.request.contextPath}/first.action">first.action</a><br>
      <a href="${pageContext.request.contextPath}/second.action">second.action</a>
</body>
</html>

图片18.png

2. Controller

@Controller
public class MyController {

    @RequestMapping("/mysubmit.action")
    public String myform(User user){
        System.out.println(user);
        return "second" ;
    }

    @RequestMapping(value = {"/first.action","/second.action"})
    public String show(){
        return "result" ;
    }

}

图片1.png

method属性

用来设置映射的请求方式 ;值是RequestMethod类型的数组;如果没有写,则没有限制,post与get都可以请求到对应的方法;如果指定了请求类型,则必须得是相应的请求才能访问到对应的方法

示例:

1. user.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>user.jsp</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/mysubmit.action"  method="post">
    用户名:<input type="text" name="user_name"><br>
    年龄:<input type="text" name="user_age" ><br>
    生日:<input type="text" name="birth"><br>
    爱好:<input type="checkbox" name="hobby" value="篮球"> 篮球
    <input type="checkbox" name="hobby" value="乒乓球"> 乒乓球
    <input type="checkbox" name="hobby" value="足球"> 足球
    <br>-------------------宠物-------------------------<br>
    宠物名称:<input type="text" name="dog.name"><br>
    宠物颜色:<input type="text" name="dog.color"><br>
    <br>-------------------宠物-------------------------<br>
    宠物名称:<input type="text" name="dogs[0].name"><br>
    宠物颜色:<input type="text" name="dogs[0].color"><br>
    <br>-------------------宠物-------------------------<br>
    宠物名称:<input type="text" name="dogs[1].name"><br>
    宠物颜色:<input type="text" name="dogs[1].color"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

图片2.png

2. Controller

@Controller
public class MyController {

    @RequestMapping(value = {"/mysubmit.action"},method = {RequestMethod.POST,RequestMethod.GET})
    public String myform(User user){
        System.out.println(user);
        return "second" ;
    }

    @RequestMapping(value = {"/first.action","/second.action"})
    public String show(){
        return "result" ;
    }
}

图片3.png

params属性

必须设置对应的请求参数和请求值才能访问到对应的内容

示例:

1. test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>index</title>
</head>
<body>
      <a href="${pageContext.request.contextPath}/test.action?name=chen&age=18">带规定参数访问</a><br>
      <a href="${pageContext.request.contextPath}/test.action">不带参数访问</a>
</body>
</html>

 

图片4.png

2. Controller

@Controller
public class TestController {
    @RequestMapping(
            value = {"/test.action"},
            method = {RequestMethod.GET,RequestMethod.POST},
            params = {"name=chen","age=18"}
    )
    public String show(){
        return "result" ;
    }
}

图片5.png

3. 运行结果

图片6.png

点击带规定参数访问的链接跳转到相应的页面

图片7.png

点击不带参数访问的链接,则会报错

图片8.png

headers属性

发送的请求头必须要与设置的请求相同时,才能够访问到对应的方法

示例:

图片9.png

ant风格地址

请求路径的一种匹配方法

1. ?:一个?匹配一个字符

匹配一个字符

图片10.png

图片11.png

 匹配两个字符,以此类推

图片12.png

图片13.png

2. *:匹配任意字符

图片14.png

图片15.png

此时第一个链接访问报404,第二三个链接成功访问

3. **:匹配多重路径

图片16.png

图片17.png

此时四个链接均能访问成功