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

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);
主站蜘蛛池模板: 米脂县| 栖霞市| 定西市| 阿巴嘎旗| 巴里| 怀化市| 河源市| 全椒县| 灵武市| 龙山县| 石屏县| 潜山县| 施甸县| 土默特左旗| 黄骅市| 胶南市| 祁连县| 进贤县| 柘城县| 咸阳市| 应用必备| 桂阳县| 吴川市| 龙胜| 曲周县| 博客| 龙南县| 呈贡县| 沧州市| 青海省| 三河市| 无为县| 洞头县| 富阳市| 建昌县| 库尔勒市| 陆良县| 玉林市| 巴南区| 左贡县| 冀州市|