Request parameter processing in spring MVC

Posted by usefulphp on Mon, 03 Jan 2022 23:26:34 +0100

Main knowledge points

  • The servlet API gets the request parameters using request getParameter("xxx")
  • In spring MVC, you only need to declare the parameter with the corresponding name in the processing method to automatically match the parameter passed in by the request and perform the corresponding type conversion (the servlet API also needs to judge the type by itself)
  • If the type does not match / cannot match, a 400 error will be reported
  • Matching rules
    • The request parameter must be consistent with the parameter name of the processing method
    • null is assigned if the parameter of the processing method is not passed in
    • If the parameter name passed in is inconsistent with the parameter name of the processing method, it can be matched through @ RequestParam("xxx")

Processing request parameter garbled

GET

On tomcat's server Add URIEncoding="utf-8" to the XML file

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="utf-8" />

POST

Configure the encoding filter characterencoding filter to solve Chinese garbled code
In spring MVC's web Add the configuration of garbled code processing interceptor in XML

<filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--        solve post Response garbled code-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
<!--        Turn on the encoding setting of request and response at the same time-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
<!--        Interception rules(/* (intercept all)-->
        <url-pattern>/*</url-pattern>
<!--        <servlet-name>according to servlet Name interception</servlet-name>-->
    </filter-mapping>

@RequestParam gets the parameters of the request

 @RequestMapping("/hehe")
    public String params01(@RequestParam(value = "xxx",defaultValue = "aqin") String name){
        System.out.println(name);
        return "/index.jsp";
    }
  • value rename parameter
  • required specifies whether the parameter must be passed in
    • true by default (no 400 error is reported)
    • false can be passed (null is automatically passed in) and no error will be reported
      • So pay attention! When declaring the passed in parameters of a function, do not use the underlying data type, because the underlying data type cannot receive null
  • defaultValue is the default value. When the passed in parameter is null, a default value will be automatically set (required=false can be omitted for this parameter)

@RequestHeader get request header information

Gets the value of the Host field in the header

	@RequestMapping(value = "/getheader",params = {"username"})
    public String params02(@RequestHeader("Host") String host){
        System.out.println(host);
        return "/index.jsp";
    }

@Cookie value gets the value in the cookie

Gets a cookie with a specified key value

@RequestMapping(value = "/getcookie",params = {"username"})
    public String params02(@CookieValue("JSESSIONID") String cookie){
        System.out.println("cookie="+cookie);
        return "/index.jsp";
    }

Get all cookie s

@RequestMapping(value = "/getcookie",params = {"username"})
    public String params03(@CookieValue Map<String,String> cookies){
        System.out.println("cookies");
        return "/index.jsp";
    }

@RequestMapping

@RequestMapping(value = "/heheda",method = {RequestMethod.POST})
    public String params02(){
        System.out.println("heheda");
        return "index.jsp";
    }

From spring 4 3 begins with a set of notes on the simplified request mode:

  • @PostMapping("heheda")
    • Here @ RequestMapping(value = "/heheda",method = {RequestMethod.POST}) is equivalent to @ PostMapping("/heheda")
  • @GetMapping()
  • @PutMapping()
  • @DeleteMapping()

params: set request parameters

  1. Some parameters must be params = {username}
  2. Some parameters params={"!username"}
  3. The parameter must be equal to a value params={"username=aqin"}
  4. The parameter must not be equal to a value params={"username!=aqin"}

headers: set the request header

  1. Must contain a value headers = {"accept language = zh CN, Zh; q = 0.9"}

Consumers: set the content type of the current request

  1. The content type of the current request must be the specified value
  2. Frequently requested content types:
    1. application/x-www-form-urlencoded form the default content type submitted by the form
    2. Content type of multipart / form data form submission file stream
    3. json content type submitted by application/json ajax
  3. Request content type mismatch: HTTP Status 415
  4. consumes={"application/x-www-form-urlencoded"}

wildcard

Mapped URLs can also support wildcards

  • ? One? Can match a single character (a-z0-9)[1]
  • *A * can match any character (a-z0-9) [any]
  • *** * match any character, any level
//http://localhost/aqin/user/123 Will match this
    @RequestMapping(value = "/user*")
    public String params05(){
        System.out.println("*");
        return "/index.jsp";
    }

//http://localhost/aqin/user/1 Will match this
    @RequestMapping(value = "/user?")
    public String params06(){
        System.out.println("?");
        return "/index.jsp";
    }

//http://localhost/aqin/1/2/3/user/ Will match this
    @RequestMapping(value = "/**/user")
    public String params07(){
        System.out.println("**");
        return "/index.jsp";
    }

Note: if the mapping has an inclusion relationship, it will be handed over to the more accurate mapping first

No uniform >? > * >**

@PathVariable

Parameter used to get URL directory level

@RequestMapping("/user/{id}")
    public String params04(@PathVariable("id") Integer id){
        System.out.println(id);
        return "/index.jsp";
    }

When the request link is http://localhost:8080/aqin/user/1012 The id in the function params04(@PathVariable("id") Integer id) will get the value of 1012.

Topics: Java Web Development Tomcat Spring MVC Annotation