How to get spring controller parameters, passing parameters and specify the jump page

Passing parameters

(1) addAttribute () method to obtain the parameter

Examples

 // addAttribute ()方法=== setAttribute ()

2 model.addAttribute("name", name); 

 

Acquisition parameters

(1) @RequestParam for acquiring parameters

Examples

. 1  @RequestParam request for obtaining parameters corresponding to the request.getParameter servlet () Method
 2  
. 3  public String Greeting (@RequestParam (name = "name", = required to false , defaultValue = "World" ) String name, the Model Model) {
 . 4  
. 5         }

Jump to specify page

(1) return for jumping

Examples

. 1   // return here corresponds to the servlet request.getRequestDispacher () forword () method, for jumping the page. 
2          return "Greeting"; // Greeting directed to src / main / resources / templates / greeting.html file , do not write the suffix is omitted

This is actually three parts of our controller class GreetingController, complete controller class, as follows

. 1  Package com.baidu.demo;
 2  
. 3  Import org.springframework.stereotype.Controller;
 . 4  Import org.springframework.ui.Model;
 . 5  Import org.springframework.web.bind.annotation.GetMapping;
 . 6  Import org.springframework.web .bind.annotation.RequestParam;
 . 7  
. 8 @Controller // indicates that the class is the controller 
. 9  public  class GreetingController {
 10  
. 11      / ** @RequestParam request for acquiring the parameters corresponding to request.getParameter servlet () method
 12 is        * the Model It is a model for storing data, corresponding to the servlet setAttribute () and getAttribute (). Model which can put POJO
13 is       * / 
14      
15      @GetMapping ( "/ Greeting") // Note @GetMapping represents a get request, corresponds to the servlet doGet () method 
16      public String Greeting (@RequestParam (name = "name", = required to false , = defaultValue "World" ) String name, the Model Model) {
 . 17          model.addAttribute ( "name", name); // the addAttribute () method of the setAttribute === ()
 18 is          
. 19        // return here corresponds to the request servlet . .getRequestDispacher () forword () method, for jumping the page 
20 is          return "Greeting"; // Greeting directed to src / main / resources / templates / greeting.html file, writing suffixes omitted 
21      }
 22 is  
23 is }

 

 

   

 

Guess you like

Origin www.cnblogs.com/hzyhx/p/11094435.html