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

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);
主站蜘蛛池模板: 洛南县| 东莞市| 隆化县| 四平市| 高阳县| 丘北县| 东阳市| 娱乐| 泗水县| 田林县| 瑞安市| 阿勒泰市| 黑水县| 珲春市| 茂名市| 阿图什市| 茂名市| 石柱| 安福县| 洛隆县| 西乌珠穆沁旗| 江达县| 新河县| 合水县| 舒兰市| 德昌县| 庄河市| 永嘉县| 南城县| 桃园市| 曲阜市| 赤壁市| 台南县| 仲巴县| 河池市| 凤凰县| 灵宝市| 洞头县| 荃湾区| 独山县| 荆州市|