MVC method returns the value data

ModelAndView role and usage

Use ModelAndView class stores result data after processing, and display of the data view. Judging from the name ModelAndView the Model represents the model, View behalf of view, the name a good explanation of the class action. After the service processor to call model layer user request has been processed, the result is stored in the class data model attributes, to return the information stored in the view class view property, and then let the ModelAndView returns the Spring MVC frame. By calling the view frame parser profile defined, the object is parsed, the final results of the data on the specified page. 

Specific role:

1 , returns the specified page

ModelAndView constructor can specify the page name returned,

Can also setViewName () to jump to a specific page method ,

2 , returns the required value

Use addObject () is provided to return the desired value, addObject () method has several different parameters can be specified, and return the default name for the object.

 

Objectl return type;

Copy the code
/*绑定下拉框*/
@RequestMapping("/prodectName")
@ResponseBody
public Object getprodectName(){
    List<Product> products = userService.getproductName();
    return products;
}
Copy the code

  String Return Value Type:

    1, if the return value is null, then a jump to request the name of the view name

    2, if the return value is specified, then a jump according to the specified view name as the return value, may carry data model, modeMap.

    3, if the return value with a forward or redirect prefix, or the corresponding request will be redirected, but can not carry data mvc data model, the data may be carried by ServletApi.

@RequestMapping ( "/ available for purchase") 
public String available for purchase (String the userName, the Model Model) { 
    // save the user name corresponding to the scope 
    model.addAttribute ( "the userName", the userName); 
    return "available for purchase"; 
}

 

Guess you like

Origin www.cnblogs.com/mayuan01/p/11829524.html