SpringMVC five ways of mass participation request

1, a conventional parameter passing mode

  The method of using the request parameters by request.getParameter ( "parameter name"), and then encapsulated into the bean

@RequestMapping("/test01")
public ModelAndView test01(HttpServletRequest request){
      String username = request.getParameter("username");
      String password = request.getParameter("password");

      System.out.println(username);
      System.out.println(password);
      return null;
}

2, simple type parameters and RequestParam comment

  If the request parameters and parameter Controller method with the same name can be directly received

  If the request type and parameter names different parameters Controller methods may be used prior to @RequestParam annotations attached parameter set corresponding to the parameter name

@RequestMapping ( "/ test02_1" )
 public ModelAndView test02_1 (username String, String password) { 
      System.out.println (username); 
      System.out.println (password); 
      return  null ; 
} 
@RequestMapping ( "/ test02_2" )
 public test02_2 ModelAndView (@RequestParam ( "username") String name, @ RequestParam (value = "password", defaultValue = "1,234,987" ) String pwd) {
       // use the parameters @RequestParam not pass null
       // required: Indicates whether the value must pass
       // defaultValue: when there is no request the parameter, SpringMVC to request the default parameter value 
      System.out.println (name); 
      System.out.println (pwd);
      return null;
}

3, the reference object by

  At this time, parameters can be automatically packaged into a parameter of the object

  Note: the same name as an attribute, and objects must request parameters

        2, and the object will be directly placed in the request scope, type the first letter of the name lowercase

     3, @ ModelAttribute parameter setting request and passed to the object bound to the page view, the key value setting

@RequestMapping ( "/ TEST03" )
 public ModelAndView TEST03 (@ModelAttribute ( "STU" ) Student Student) {
     / * object can be used as a parameter of the method, while accepting a plurality of receiving reception request parameters 
      SpringMVC be based on matching the same name, the injection request parameter value corresponding object properties 
      @ModelAttribute a name key value * / 
      System.out.println (Student); 
      ModelAndView Music Videos = new new ModelAndView (); 
      mv.setViewName ( "test2" );
       return Music Videos; 
}

 4, the array type parameters and a set of List

  The current page table of parameters coming from the same parameter name, parameter values ​​of a plurality of different parameters, can be packaged directly into the array method of the type of parameter may be packaged directly into the collection property of the object.

  For example came when bulk delete parameters.

@RequestMapping ( "/ test04" )
 public ModelAndView test04 (ID String []) {
     / * for the same parameter name request plurality of parameters, the array can be directly used as the shape parameter reception process 
      can be set using the object attribute of * / 
      for (String I: ID) { 
          System.out.println (I); 
      } 
      return  null ; 
}
@RequestMapping("/test05")
public ModelAndView test05(Student student){
      System.out.println(student.getId().size());
      for (String i : student.getId()) {
          System.out.println(i);
      }
      return null;
}

5, using Restful

  Restful is a software architecture style, strictly speaking is a coding style, that make full use of the HTTP protocol itself semantics which provides a set of design principles and constraints.

  Mainly used for client and server interaction software like the style of the software can be more concise, more structured and easier to implement caching mechanisms. After background, @RequestMapping labels can parameter name} {parameter passing mode, while the need to add annotations @PathVarible parameter before, if the request address reception is localhost: 8080 / delete / 3

@RequestMapping("/delete/{id}")
public ModelAndView test4(@PathVariable("id")Long id){
    System.out.println("delete");
    System.out.println(id);
    return null;
}

 

Guess you like

Origin www.cnblogs.com/xfdhh/p/11517534.html