SpringMVC -- @RequestMapping用法

继续http://blog.csdn.net/gaopeng0071/article/details/47447811章节,

了解@RequestMapping更多其他用法,查看@RequestMapping参数如图
这里写图片描述

用法如下

value:

In a Servlet environment: the path mapping URIs (e.g. “/myPath.do”). Ant-style path patterns are also supported (e.g. “/myPath/*.do”). At the method level, relative paths (e.g. “edit.do”) are supported within the primary mapping expressed at the type level.

  • 支持”/myPath.do”用法
  • 支持ANT风格的URL,如”/myPath/*.do”
    /user/aaa/createUser、/user/bbb/createUser 等 URL
    /user/**/createUser: 匹配 –
    /user/createUser、/user/aaa/bbb/createUser 等 URL
    /user/createUser??: 匹配 –
    /user/createUseraa、/user/createUserbb 等 URL

代码

    @RequestMapping(value = "/*/sayHello")
    public String sayHello() {
        System.out.println("hello world!!!");
        return "success";
    }
<a href="springMVC/aaa/sayHello.do">Test HelloWorld</a>

method:

The HTTP request methods to map to, narrowing the primary mapping: GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE.

主要使用GET、POST两种请求方式。

代码

    @RequestMapping(value = "/testRequestMethod", method = RequestMethod.POST)
    public String testRequestMethod() {
        System.out.println("testRequestMethod");
        return "success";
    }
    <form action="springMVC/testRequestMethod.do" method="post">
        <input type="submit" value="testRequestMethod" />
    </form>

params:

Same format for any environment: a sequence of “myParam=myValue” style expressions, with a request only mapped if each such parameter is found to have the given value. Expressions can be negated by using the “!=” operator, as in “myParam!=myValue”. “myParam” style expressions are also supported, with such parameters having to be present in the request (allowed to have any value). Finally, “!myParam” style expressions indicate that the specified parameter is not supposed to be present in the request.

  • !param1: 表示请求不能包含名为 param1 的请求参数
  • param1 != value1: 表示请求包含名为 param1 的请求参数,但其值不能为 value1
  • {“param1=value1”, “param2”}: 请求必须包含名为 param1 和param2的两个请求参数,且 param1 参数的值必须为 value1

代码

    @RequestMapping(value = "/testParam", params = "userId='userName' ")
    public String testParam() {
        System.out.println("params userId is not null");
        return "success";
    }
    <a href="springMVC/testParam.do?userId=userName ">testParam</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值