SpringMVC_Common Annotations_hehe.employment.over.37.3

37.8 SpringMVC_Common annotations

37.8.1 RequestParam

  • effect:
    • Assign the parameter with the specified name in the request to the formal parameter in the controller.
  • Attributes:
    • value: The name in the request parameter.
    • required: Whether this parameter must be provided in the request parameters. Default value: true. Indicates that it must be provided, if not provided, an error will be reported.
  • Example:
 @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name="name") String username){
    
    
        System.out.println("执行了...");
        System.out.println(username);
        return "success";
    }
<a href="anno/testRequestParam?name=哈哈">RequestParam</a>

37.8.2 RequestBody

  • effect:
    • Used to obtain the content of the request body. Use directly to get data with key=value&key=value... structure.
    • The get request method is not applicable.
  • Attributes:
    • required: Whether there must be a request body. The default value is: true. When the value is true, the get request method will report an error. If the value is false, the get request is null.
    /**
     * 获取到请求体的内容
     * @return
     */
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
    
    
        System.out.println("执行了...");
        System.out.println(body);
        return "success";
    }
    <form action="anno/testRequestBody" method="post">
        用户姓名:<input type="text" name="username" /><br/>
        用户年龄:<input type="text" name="age" /><br/>
        <input type="submit" value="提交" />
    </form>

37.8.3 PathVaribale

  • effect:
    • Used to bind placeholders in the url. For example: /delete/{id} in the request url, this {id} is the url placeholder.
    • The url support placeholder was added after spring3.0. It is an important sign that springmvc supports rest style URL.
  • Attributes:
    • value: Used to specify the placeholder name in the url.
    • required: Whether a placeholder must be provided.
  • Example:
    /**
     * PathVariable注解
     * @return
     */
    @RequestMapping(value="/testPathVariable/{sid}")
    public String testPathVariable(@PathVariable(name="sid") String id){
    
    
        System.out.println("执行了...");
        System.out.println(id);
        return "success";
    }
    <a href="anno/testPathVariable/10">testPathVariable</a>

37.8.4 Example based on HiddentHttpMethodFilter

  • effect:
    • Because the browser form only supports GET and POST requests, and DELETE, PUT and other methods do not support, Spring3.0 added a filter, you can change the browser request to the specified request method, and send it to our controller method , Making it support GET, POST, PUT and DELETE requests.
  • Instructions:
  • Step 1: Configure the filter in web.xml.
  • Step 2: The request method must use post request.
  • Step 3: Provide the _method request parameter as required. The value of this parameter is the request method we need.

37.8.5 RequestHeader

  • effect:
    • Used to get the request message header.
  • Attributes:
    • value: Provide the name of the message header
    • required: whether this message header must be present
  • Note:
    • It is generally not used much in actual development.
 /**
     * 获取请求头的值
     * @param header
     * @return
     */
    @RequestMapping(value="/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value="Accept") String header) throws IOException {
    
    
        System.out.println("执行了...");
        System.out.println(header);
         return "success";     
    }

   <a href="anno/testRequestHeader">RequestHeader</a>

37.8.6 CookieValue

  • effect:
    • Used toSpecify the value of the cookie namePass in controller method parameters.
  • Attributes:
    • value: Specifies the name of the cookie.
    • required: Whether this cookie is required.
  • Example:
/**
     * 获取Cookie的值
     * @return
     */
    @RequestMapping(value="/testCookieValue")
    public String testCookieValue(@CookieValue(value="JSESSIONID") String cookieValue){
    
    
        System.out.println("执行了...");
        System.out.println(cookieValue);
        return "success";
    }
    <a href="anno/testCookieValue">CookieValue</a>

37.8.7 ModelAttribute

  • effect:
    • This annotation is newly added after SpringMVC4.3. It can be used to modify methods and parameters.
    • Appears on the method, which meansThe current method will be executed before the controller method is executed. It can modify methods that do not have a return value, or methods that have a specific return value.
    • Appears on the parameter,Get the specified data and assign values ​​to the parameters
  • Attributes:
    • value: The key used to obtain the data. The key can be the attribute name of the POJO or the key of the map structure.
  • Application scenarios:
    • When the form submission data is not complete entity data, ensure that the fields without submitted data use the original data of the database object.
  • E.g:
    • When we edit a user, the user has a creation information field, and the value of this field is not allowed to be modified. When submitting the form data, there must be no content of this field. Once updated, the content of this field will be set to null. At this time, you can use this annotation to solve the problem.

37.8.8 SessionAttribute

  • effect:
    • Used to execute parameter sharing between controller methods multiple times.
  • Attributes:
    • value: used to specify the name of the stored attribute
    • type: Used to specify the type of data stored.
  • Example:
 /**
     * SessionAttributes的注解
     * @return
     */
    @RequestMapping(value="/testSessionAttributes")
    public String testSessionAttributes(Model model){
    
    
        System.out.println("testSessionAttributes...");
        // 底层会存储到request域对象中
        model.addAttribute("msg","美美");
        return "success";
    }

    /**
     * 获取值
     * @param modelMap
     * @return
     */
    @RequestMapping(value="/getSessionAttributes")
    public String getSessionAttributes(ModelMap modelMap){
    
    
        System.out.println("getSessionAttributes...");
        String msg = (String) modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }

    /**
     * 清除
     * @param status
     * @return
     */
    @RequestMapping(value="/delSessionAttributes")
    public String delSessionAttributes(SessionStatus status){
    
    
        System.out.println("getSessionAttributes...");
        status.setComplete();
        return "success";
    }

    <a href="anno/testSessionAttributes">testSessionAttributes</a>
    <a href="anno/getSessionAttributes">getSessionAttributes</a>
    <a href="anno/delSessionAttributes">delSessionAttributes</a>

Guess you like

Origin blog.csdn.net/qq_44686266/article/details/114839313