SpringMVC-@RequestMapping相关属性

value 属性

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>@RequestMapping相关属性</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/first.action">第一个请求</a>
<br/>
<a href="${pageContext.request.contextPath}/second.action">第二个请求</a>
</body>
</html>

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

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

测试方式启动项目之后发送请求即可。

method 属性

用来设置映射的请求方式,值是 RequestMethod 类型的数组。

如果没有写,则没有限制,POST 与 GET 都可以请求到对应的方法,如果指定了请求类型,则必须得是相应的请求类型才能访问到对应的方法。

首先只指定一个请求类型也就是 POST 我们接下来开始发送请求如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>@RequestMapping相关属性</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/testMethod.action" method="post">
    <input type="submit" value="提交"/>
</form>
</body>
</html>

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

    @RequestMapping(value = {"/testMethod"}, method = {RequestMethod.POST})
    public String show() {
        return "/first";
    }
}

测试方式启动项目之后发送请求即可,在改为 GET 然后在发送请求你即可看到效果。

测试方式启动项目之后发送请求即可。

params 属性

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>@RequestMapping相关属性</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/testMethod.action?name=BNTang&age=23" method="post">
    <input type="submit" value="提交"/>
</form>
</body>
</html>

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

    @RequestMapping(
            value = {"/testMethod"},
            method = {RequestMethod.POST},
            params = {"name=BNTang", "age!=24"})
    public String show() {
        System.out.println("BNTang");
        return "/first";
    }
}

headers 属性

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

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

    @RequestMapping(
            value = {"/testMethod"},
            method = {RequestMethod.POST},
            params = {"name=BNTang", "age!=24"},
            headers = {"Host=localhost:8080", "Referer=http://localhost:8080/index.jsp"})
    public String show() {
        System.out.println("BNTang");
        return "/first";
    }
}

ant 风格地址

ant风格,请求路径的一种匹配方法,

通配符

通配符名称 作用
? 一个 ? 匹配一个字符
* 匹配任意字符
** 匹配多重路径

? 通配符的示例如下所示:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>@RequestMapping相关属性</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/ant/a/BNTang.action">测试Ant路径</a>
</body>
</html>

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

    @RequestMapping("/ant/?/BNTang")
    public String show() {
        System.out.println("BNTang");
        return "/first";
    }
}

* 通配符的示例如下所示:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>@RequestMapping相关属性</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/ant/abc/BNTang.action">测试Ant路径</a>
</body>
</html>

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

    @RequestMapping("/ant/*/BNTang")
    public String show() {
        System.out.println("BNTang");
        return "/first";
    }
}

** 通配符的示例如下所示:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>@RequestMapping相关属性</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/ant/abc/defg/hijk/BNTang.action">测试Ant路径</a>
</body>
</html>

/**
 * @author: BNTang
 */
@Controller
public class MyFirstController {

    @RequestMapping("/ant/**/BNTang")
    public String show() {
        System.out.println("BNTang");
        return "/first";
    }
}
posted @ 2020-11-08 14:30  BNTang  阅读(60)  评论(0编辑  收藏  举报