官术网_书友最值得收藏!

Response as an instance of ModelAndView

ModelAndView is a container object to hold both Model and View. With ModelAndView as a return object, the controller returns the both model and view as a single return value. The model is a map object which makes it possible to store key-value pairs. The following code sample represents the usage of ModelAndView in a Controller:

    @Controller
@RequestMapping("/account/*")
public class UserAccountController {

@PostMapping("/signup/process")
public ModelAndView processSignup(ModelMap model, @RequestParam("nickname") String nickname, @RequestParam("emailaddress")
String emailAddress, @RequestParam("password") String password) {
model.addAttribute("login", true);
model.addAttribute("nickname", nickname);
model.addAttribute("message", "Have a great day ahead.");
return new ModelAndView("index", model);
}
}

The following code samples represent the different ways in which an instance of ModelAndView is returned with different sets of information:

    // Will result in display of index.jsp page
return new ModelAndView("index");

// Will result in display of index.jsp page.
//The JSP page could consist of code such as "Hello ${name}"
//which will get displayed as "Hello Calvin Hobbes"

return new ModelAndView("index", "name", "Calvin Hobbes");

// Will result in display of index.jsp page.
// The JSP page could consist of code such as
//"Hello ${model.firstName} ${model.lastName}"
//which will get displayed as "Hello Calvin Hobbes"

UserInfo userInfo = new UserInfo();
userInfo.setFirstName("Calvin");
userInfo.setLastName("Hobbes");
return new ModelAndView("index", "model", userInfo);

// Will result in display of index.jsp page.
// The JSP page could consist of code such as "Hello ${name}"
// which will get displayed as "Hello Calvin Hobbes"

Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "Calvin Hobbes");
return new ModelAndView("index", map);
主站蜘蛛池模板: 江津市| 厦门市| 射洪县| 栾川县| 定安县| 青神县| 孝昌县| 安乡县| 临沧市| 溆浦县| 吉林市| 苏尼特左旗| 宜城市| 佛山市| 青河县| 阿拉善左旗| 根河市| 延川县| 富平县| 大安市| 奉化市| 丹棱县| 大洼县| 淮南市| 丹巴县| 闽清县| 崇礼县| 得荣县| 宁远县| 平利县| 温宿县| 高尔夫| 客服| 茂名市| 平顺县| 清水县| 平江县| 时尚| 洪泽县| 普宁市| 墨竹工卡县|