SpringMVC- request parameters

First, access to a single parameter:

@RequestParam(value = "",required = true,defaultValue = "")

  value = "": specify the parameters to get the key

  required = true: This argument is required

  defaultValue = "": the default values, not null with default

"?" Acquisition request parameter value following path: () 1. @RequestParam

@RequestMapping("/handle")
public String handle(@RequestParam("name") String name) {
    return "" ;
}

2. @PathVariable (): Get the path key value

@RequestMapping("/handle/{name}")
public String handle(@PathVariable("name") String name) {
    return "" ;
}

3. @RequestHeader (): Get the value of a key in the request header

@RequestMapping("/handle")
public String handle(@RequestHeader("User-Agent") String userAgent) {
return "" ;
}

4. @CookieValue():

@RequestMapping("/handle")
public String handle(@CookieValue("JESSIONID") String jid) {
    return "" ;
}

Second, obtaining parameters vo: Cascade properties can also be encapsulated

@RequestMapping("/handle")
public String handle(Member member) {
    return "" ;
}

Third, the incoming native API:

@RequestMapping("/handle")
public String handle(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
    return "" ;
}

  Although the method can get built-in objects, but also needs to take into account the rest of the built-in objects, can be obtained by RequestContextHolder:

((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest() ;
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse() ;

Fourth, the request garbled:

  GET Request: Modify Tomcat server.xml configuration file, set URIEncoding = "UTF-8" port 8080

  POST request: Set request.setCharacterEncoding ( "UTF-8") prior to the first request, SpringMVC encoding filter is provided: CharacterEncodingFilter

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- encoding:解决POST请求乱码 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- :forceEncoding响应乱码 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

Guess you like

Origin www.cnblogs.com/luliang888/p/11074768.html