(transfer) Spring MVC parameter passing and form interaction

Reprinted from: http://blog.csdn.net/daybreak1209/article/details/50667079

 

The characteristics of the MVC pattern lie in the hierarchical management of pages, business logic, and entities, how to transfer data between the three layers, and what makes Spring MVC unique compared to other web frameworks.

In the MVC framework mode, most of the parameters are passed by sending a request request to the controller controller. The controller calls the background service business, obtains data, and echoes the View interface. So the main parameter passing is between Controller and View. Let's introduce how to send and receive data between Controller and View.

1. Controller receives View parameters

1. Through HttpServletRequest

Parameter transfer between page and controller through HttpServletRequest is a typical Servlet parameter transfer method. For example, using Servlet as the role of controller in Web development, by implementing the servlet interface and rewriting the doGet\doPost method, the two essential parameters are HttpServletRequest and HttpServletResponse. So using HttpServletRequest to accept request request parameters is not a way specific to Spring MVC.

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")  
  2.        public String toPerson(HttpServletRequest request){  
  3.        String result = request.getParameter("name");  
  4.        System.out.println(result);  
  5.        return "index";  
  6.       }  

String  studentName=request.getParameter("name");

When testing, the browser passes the name=max1209 parameter by visiting the URL: http://localhost:8091/ springMVC/getName.do ? Name=max1209  .   

2. Directly define the parameter name

The controller receives the interface parameters, and can also directly specify the parameter name

1) Single parameter

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")   
  2.   
  3.  public String getName(String name){   
  4.   
  5.         System.out.println(name);  
  6.   
  7.        return "/index";   
  8.   
  9.  }  

       When testing, the browser passes the name=max1209 parameter by visiting the URL: http://localhost:8091/springMVC/ getName .do?name=max1209  .    
      2) Multiple parameters

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")   
  2.   
  3.  public String getName(String name, Integer age, String address, Date birthday){   
  4.   
  5.        System.out.println(name + " "+ age + " "+ address + " " +birthday);  
  6.   
  7.        return "/index";   
  8.   
  9.  }  

When testing, the browser passes in multiple parameters by visiting the URL: http://localhost:8091/springMVC/ getName .do?name=max1209 &age =3 0 &address =London .          

Notice

1) It is required that the parameter passed in by the foreground and the parameter name in the controller method must be the same, so that the parameter value can be successfully obtained.

2) Parameter type, if the front desk enters and leaves the String type, the background method parameter is Integer, and the "123" number string is passed in, SpringMVC can automatically convert it into

3) The time type parameter needs to specify the time format

3. Through entity integration

When there are too many interface parameters, it is too cumbersome to receive in the form of the parameter name specified in 2. Parameters can be integrated into an entity to receive. For example: define a Person entity to accept parameter values ​​such as person's name, age, and sex.

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")  
  2. public String getName(Person person){  
  3.     System.out.println(person);  
  4.     return "index";  
  5. }  

When testing, the browser passes in multiple parameters by visiting the URL: http://localhost:8091/springMVC/ getName .do?name=max1209 &age =3 0 &address =London .                

Using entities to receive parameters requires attention:

1. URL delivery

2. Parameter list, if there are two entities, person and user, both have name and age attributes, no matter which entity, as long as the incoming parameter name can match the set method in the entity, the entity assignment can be changed. That is to say, if the url passes in the name and age parameters, and the person and User entities also have these two parameters, then both entities can inject the name and age parameter values. 

4. Array type parameter reception

This applies to the page, for example, when there are multiple checkboxes, the names are the same, and if multiple checkbox values ​​are passed in, the Controller needs to use an array to receive parameters.

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")  
  2. public String getName(String[] name){  
  3.     for(String result : name){  
  4.         System.out.println(result);  
  5.     }  
  6.     return "index";  
  7. }  

When testing, the browser passes the array parameter by visiting the URL: http://localhost:8091/springMVC/ getName .do?name=max1209 &name =li&name =zzz . 2. How does the Controller return business data to the View for display               

1. Return through ModelAndView

Encapsulate the data into an entity and return the interface . In fact, the person object is put into the map and encapsulated into the response object through ModelAndView, so the front desk can directly obtain the attribute through the map key value.

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")  
  2. public ModelAndView getName() throws Exception{  
  3.     Person person = new Person();  
  4.     person.setName("james");  
  5.     person.setAge(28);  
  6.     person.setAddress("maami");  
  7.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  8.     Date date = format.parse("1985-04-22");  
  9.     person.setBirthday(date);  
  10.     Map<String, Object> map = new HashMap<String, Object>();  
  11.     map.put("p", person);  
  12.     return new ModelAndView("index", map);  
  13. }  

2. Through Map

Add the map parameter to the method, and the ModelAndView view manager will automatically assemble the map parameter into the request. The method can still return the person entity to the interface by returning a String.

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")  
  2. public String getName(Map<String, Object> map) throws Exception{  
  3.     Person person = new Person();  
  4.     person.setName("james");  
  5.     person.setAge(28);  
  6.     person.setAddress("maami");  
  7.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  8.     Date date = format.parse("1985-04-22");  
  9.     person.setBirthday(date);  
  10.     map.put("p", person);  
  11.     return "index";  
  12. }  

3. Use the Model object directly

The most common and most convenient is to return directly through the Model object

 

[java] view plain copy
 
  1. @RequestMapping("/getName.do")  
  2. public String getName(Model model) throws Exception{  
  3.     Person person = new Person();  
  4.     person.setName("james");  
  5.     person.setAge(28);  
  6.     person.setAddress("maami");  
  7.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  8.     Date date = format.parse("1985-04-22");  
  9.     person.setBirthday(date);  
  10.     // put the parameter value into the request class  
  11.     model.addAttribute("p", person);  
  12.     return "index";  
  13. }  

These three methods can be obtained by the front desk through the JSTL+EL expression to obtain the key-value in the addAttribute to obtain the person entity data

[java] view plain copy
 
  1. <span style="font-family:Comic Sans MS;"> <body>  
  2.         <h1>${p.name}</h1>  
  3.         <h1>${p.age}</h1>  
  4.         <h1>${p.sex}}</h1>  
  5.         <h1>${p.address}</h1>  
  6.     </body></span>  

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326168402&siteId=291194637
Recommended