Spring MVC learning URL parameter transfer details

  • 2020-05-27 05:29:28
  • OfStack

In learning Spring Mvc, it is necessary to first understand several key parameters:

@ Controller:

Annotate on class, this class will program a controller, and at project startup Spring will automatically scan this class and perform the corresponding URL routing mapping.


@Controller

public class UserAction{ } 

@RequestMapping

Specify the URL mapping path. If RequestMapping is configured on the controller and the request method is also configured on the path, the mapped path will be the superposition of the two paths. Common mapping is RequestMapping(" url.html ").

Configure the mapping path:


@Controller
public class UserAction 
{
  @RequestMapping(value = "/get_alluser.html")
  public ModelAndView GetAllUser(String Id)
  {
  }
} 

The above configuration map

http: / / * * * : 8080: web1 / get_alluser html:

If you add @RequestMapping (value = "/user") to @Controller, the mapping path becomes

http://***:8080:web1/user/get_alluser.html

@ResponseBody

Returns the string corresponding to the annotation method directly

@RequestParam

Automatically map the parameters corresponding to URL to the values above Action. RequestParam is required by default.

@PathVariable

Gets the URL mapping parameter in the @RequestMapping configuration specified format


 /*
   *   Direct output  HTML Or, JSON  string 
   *   Request path :
   *    /web1/urlinfo/getcontent.html?key=rhythmk
   *   /web1/urlinfo/getcontent.json?key=rhythmk
   * */
  @ResponseBody
  @RequestMapping(value = "/getcontent.**")
  public String GetContent(
      @RequestParam("key") String key,
      @RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
    System.out.println("getcontent  Is called ");
    String result = " Return content directly  - key:" + key + ",key2:" + key2;
    System.out.println(result);
    return result;
  } 


 /*
   * RequestMapping  support  Ant  In the style of URL configuration   : 
   *  Request path: 
   *   /urlinfo/geturlant/config.html?key=adddd
   */
  @ResponseBody
  @RequestMapping(value = "/geturlant/**.html")
  public String getUrlAnt(HttpServletRequest request) {
    String result = "? The following parameters are: " + request.getQueryString();
    return result;
  } 

 /*
   *  Configure the specified format URL , mapping to the corresponding parameter 
   *   Request path: /web1/urlinfo/geturlparam/12_123.html
   *   
   * */
  
  @RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
  public ModelAndView getUrlParam(@PathVariable("id") String id,
      @PathVariable("menuId") String menuId) {
    ModelAndView mode = new ModelAndView(ShowMsg);
    mode.addObject("msg", " To get to Id:" + id + ",menuId:" + menuId);
    return mode;
  } 

  /*
   *  Only receive Post  request 
   */
  @ResponseBody
  @RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
  public String UrlMethod(@RequestParam String id) {
    return " Can only be Post request , To get to Id:" + id;
  } 

  /*
   *   write  cookie
   * */ 
  @RequestMapping("/writecookies.html")
  public ModelAndView writeCookies(@RequestParam String value,
      HttpServletResponse response) {

    response.addCookie(new Cookie("key", value));
    ModelAndView mode = new ModelAndView(ShowMsg);
    mode.addObject("msg", "cookies  Write to successful ");
    return mode ;
  } 


  /*
    *  through  @CookieValue  Get the corresponding key The value of the 
    * */
  @RequestMapping("/getcookies.html")
  public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
    ModelAndView mode = new ModelAndView(ShowMsg);
    mode.addObject("msg", "cookies=" + cookvalue);
    return mode;
  } 

  /* 
   *  will  Servlet Api  Pass in as a parameter  
   *   Can be found in action Medium direct use  HttpServletResponse . HttpServletRequest
   * */
  @RequestMapping("/servlet.html")
  public String Servlet1(HttpServletResponse response,
      HttpServletRequest request) {

    Boolean result = (request != null && response != null);
    ModelAndView mode = new ModelAndView();
    mode.addObject("msg", "result=" + result.toString());
    return ShowMsg;

  } 

  /*
   *   According to the URL The parameter passed in instantiates the object 
   *  
   *   Such as:  http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad
   * */
  @RequestMapping("getobject.html")
  public ModelAndView getObject(UserInfo user) {
    String result = " The user ID : " + user.getUserId().toString() + ", The user name :"
        + user.getUserName().toString();
    ModelAndView mode = new ModelAndView(ShowMsg);
    mode.addObject("msg", "result=" + result.toString());
    return mode;
  } 

Realize page jump:


@Controller
public class UserAction 
{
  @RequestMapping(value = "/get_alluser.html")
  public ModelAndView GetAllUser(String Id)
  {
  }
} 
0

Directly back to JSON

The requested URL address 1 must end with.json, otherwise exception

Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource by this request only of characteristics not acceptable according to request "accept" headers ()

Return entity:


@Controller
public class UserAction 
{
  @RequestMapping(value = "/get_alluser.html")
  public ModelAndView GetAllUser(String Id)
  {
  }
} 
1

Back action


@Controller
public class UserAction 
{
  @RequestMapping(value = "/get_alluser.html")
  public ModelAndView GetAllUser(String Id)
  {
  }
} 
2

Request:

/web1/urlinfo/getuser.json

Output:


 {"userId":100,"userName":" Wang kun "}

Related articles: