SpringMVC @RequestMapping annotation

@RequestMapping is used to map requests: RequestMapping can modify methods and classes

1) SpringMVC uses the @RequestMapping annotation to specify which URL requests can be processed for the control;

2) The @RequestMapping annotation can be used in the control class definition and method definition

---Class definition: Provide preliminary request mapping information, relative to the root directory of the WEB application.

---Method: Provides further subdivision mapping information, relative to the URL at the class definition. If @RequestMapping is not marked at the class definition, the URL marked at the method is relative to the root directory of the WEB application.

3) After the DispatcherServlet intercepts the request, it determines the processing method corresponding to the request through the mapping information provided by @RequestMapping on the controller.

Example: Modify the HelloWord.java class based on the demo project built in the previous article.

copy code
package com.dx.springlearn.handlers;

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

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";
    
    @RequestMapping("/hello")
    public String hello() {
        System.out.println("hello word...");
        return SUCCESS;
    }
}
copy code

At this point, the link path in index.jsp is edited as:

<a href="class_requestmapping/hello">hello action</a>

 @RequestMapping specifies how the method is requested:

1) @ReuqestMapping can map requests using request methods, request parameters and request headers in addition to request URL mapping .

2) The value, method, params and headers of @RequestMapping represent the mapping conditions of the request URL, request method, request parameters and request header respectively. change.

3) params and headers support simple expressions:

--- param1: Indicates that the request must contain a request parameter named param1

--- !param1: Indicates that the request cannot contain a request parameter named param1

--- param1 != value1:表示请求包含名为param1的请求参数,但其值不能为value1

--- {"param1=value1","param2"}:请求必须包含名为param1和param2的两个请求参数,且param1参数的值必须为value1

在HelloWord.java中添加testMethod方法:

copy code
package com.dx.springlearn.handlers;

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

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";

    @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    public String testMethod() {
        System.out.println("test method");
        return SUCCESS;
    }

    @RequestMapping("/hello")
    public String hello() {
        System.out.println("hello word...");
        return SUCCESS;
    }
}
copy code

在index.jsp中指定请求testMethod方法的html脚本:

    <form id="form_testMethod" name="form_testMethod" method="POST"
        action="class_requestmapping/testMethod">
        <button name="submit" id="submit">test method</button>
    </form>

 @RequestMapping指定请求参数和请求头

在HelloWord.java中添加testParamsAndHeaders方法:

copy code
package com.dx.springlearn.handlers;

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

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";

    @RequestMapping(value="/testParamsAndHeaders",params={"username","password!=23456"},headers={"Accept-Language=zh-CN,zh;q=0.8"})
    public String testParamsAndHeaders() {
        System.out.println("test params and headers");
        return SUCCESS;
    }

    @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    public String testMethod() {
        System.out.println("test method");
        return SUCCESS;
    }

    @RequestMapping("/hello")
    public String hello() {
        System.out.println("hello word...");
        return SUCCESS;
    }
}
copy code

修改index.jsp,添加请求链接:

<a href="class_requestmapping/testParamsAndHeaders?username=abc&password=12345">testParamsAndHeaders</a>

@RequestMapping支持Ant路径方式:

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

--- ?:匹配文件名中的一个字符;

--- * :匹配文件名中的任何个字符;

--- **:匹配多层路径

2)Ant风格示例:

--- /user/*/createuser:配置/user/aaa/createuser、user/bb/createuser等URL

--- /user/**/createuser:匹配/user/createuser、/user/abc/ab/createuser等URL

--- /user/createuser??:配置/user/createuseraa、/user/createuserbc等URL

测试方法:

copy code
    @RequestMapping("/testAnt/*/createuser")
    public String testAnt(){
        System.out.println("test ant");
        return SUCCESS;
    }
    
copy code

index.jsp支持的链接方式:

<a href="class_requestmapping/testAnt/* (can be written)/createuser">testAntPath</a>//index.jsp

http://localhost:8080/SpringMVC_01/class_requestmapping/testAnt/oiasdkj/createuser

http://localhost:8080/SpringMVC_01/class_requestmapping/testAnt/oxd/createuser

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325457578&siteId=291194637