SpringMVC 5. RequestParam,RequestHeader,CookieValue 注解

 Object-oriented is oriented to the king, and the code is not the same as the king. *^o^*

RequestParam, RequestHeader, CookieValue annotations


1 Use @RequestParam to bind request parameter values

1.1 Use @RequestParam at the input parameter of the processing method to make the request parameter

number passed to the request method

  • value: parameter name
  • required: Whether it is required. The default is true, which means that the corresponding parameters must be included in the request parameters. If they do not exist, an exception will be thrown

controller class:

@RequestMapping("/springmvc")
@Controller
public class RequestParamTest {

    private static final String SUCCESS = "success" ;

    /*@RequestParam 来映射请求参数. value 值即请求参数的参数名 required 该参数是否必须. 默认为 true
     *defaultValue 请求参数的默认值
     */
    @RequestMapping(value = "/testRequestParam")
    public String testRequestParam(@RequestParam("username") String username , @RequestParam(value = "age",required = false) Integer age){
        System.out.println("testRequestParam , username:"+username +","+" age:"+age);
        return SUCCESS ;
    }
}

jsp page:

<a href="/springmvc/testRequestParam?username=Tom&age=16">RequestParam page</a>

2 Use @RequestHeader to bind the attributes of the request header

The request header contains several attributes, and the server can obtain the information of the client accordingly. Through @RequestHeader, the attribute value in the request header can be bound to the input parameter of the processing method.

Add to the cotroller class:

@RequestMapping(value = "/testRequestHeader")
    public String testRequestParam(@RequestHeader(value = "Accept-Language") String al){
        System.out.println("testRequestHeader,Accept-Language:"+ al);
        return SUCCESS ;
    }

jsp page:

 <a href="/springmvc/testRequestHeader">RequestHeader page</a>

@CookieValue allows the processing method input parameter to bind a cookie value

Add to the cotroller class:

 @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue("JSESSIONID") String sessionID){
        System.out.println("testCookieValue,JSESSIONID:"+sessionID);
        return SUCCESS ;
    }

jsp page:

<a href="/springmvc/testCookieValue">testCookieValue page</a>
   Daniel, don't watch silently. Log in to help me comment! *^o^*

Guess you like

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