- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 246字
- 2021-07-02 19:38:20
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);
推薦閱讀
- Mobile Web Performance Optimization
- 深入理解Bootstrap
- Python自然語言處理實戰:核心技術與算法
- Visual Basic編程:從基礎到實踐(第2版)
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第2版)
- Data Analysis with Stata
- Jupyter數據科學實戰
- C++寶典
- Learning Ionic
- C語言程序設計教程
- SQL Server 2008實用教程(第3版)
- Python程序設計案例教程:從入門到機器學習(微課版)
- Python for Secret Agents
- Instant Highcharts
- 軟件方法(上):業務建模和需求(第2版)