Spring MVC url Submit parameters and get parameters

  • 2020-06-19 10:16:24
  • OfStack

Ordinary URL submit parameters

The format url is: ES5en. do? param1 = mahc & param2=8888.00

The HelloController object above needs to be added as follows:


/** 
   * Spring MVC URL Submit parameters  
   * @param name 
   * @return 
   */ 
  @RequestMapping("/param") 
  public ModelAndView getInfo(@RequestParam("name") String name){ 
     
    String str = name + " Spring MVC The sample "; 
    return new ModelAndView("message", "str", str); 
  } 

The url format for accessing the method is: param? name=hoking(Get mode). This is a very common way to submit. Bind the request parameter a to the variable a with the annotation @RequestParam. When the request parameter a does not exist, an exception can be made by setting the property required=false, for example: @RequestParam (value="a", required=false). As mentioned above, get the parameters submitted through name.

RESTful style URL parameters

Next, let's take a look at the Restful style. HTTP request method in RESTful Web typical application service resources GET PUT POST DELETE1 URI group resources, such as http: / / example com URI/resources/a single resource, such as http: / / example com/resources / 142. Read the following article for more information.

The specific implementation needs to add the HelloController object in the above method as follows:


/** 
   * Spring MVC  support RESTful In the style of URL parameter  
   * 
   * @return 
   */ 
  @RequestMapping("/index/{username}") 
  public String getMessage(@PathVariable("username") String username){ 
    System.out.println(username); 
    return "message"; 
  } 

@PathVariable was used above. The difference between PathVariable and RequestParam is that

When using the @RequestMapping URI template style mapping, i.e. someUrl/{paramId}, the paramId can bind the values it passes to the method parameters using the @Pathvariable annotation.

The FORMAT for accessing the method is url /mahoking. @es81EN is used to get the dynamic parameters in the request url, 10 points convenient. mahoking is the dynamic value of username.

The getMessage() method above returns the String object, which represents the jump address of the page and does not contain an extension (suffix). In this case, the message.jsp page.


Related articles: