/**
* 目標方法的返回值可以是ModelAndView類型 其中可以包含視圖和模型信息
* SpringMVC會把ModelAndView的model中數據放入到request域對象中。
*
* @return
*/
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView modelAndView = new ModelAndView(SUCCESS);
// 將模型數據添加到ModelAndView中
modelAndView.addObject("time", new Date());
return modelAndView;
}
1.SpringMVC的Handler方法可以支持ServletAPI的哪些原生的參數類型
/**
* 可以使用Servlet原生的API作爲目標方法的參數,具體支持以下類型
* HttpServletRequest
* HttpServletResponse
* HttpSession
* java.security.Principal
* Locale
* InputStream
* OutputStream
* Reader
* Writer
*
*
*/
@RequestMapping("testServletAPI")
public void testServletAPI(HttpServletRequest request,HttpServletResponse response,Writer out) throws IOException{
System.out.println("test testServletAPI: "+request+" ----"+response);
out.write("testServletAPI finished!");
// return SUCCESS;
}
2.處理模型數據
2.1ModelAndView
/**
* 目標方法的返回值可以是ModelAndView類型 其中可以包含視圖和模型信息
* SpringMVC會把ModelAndView的model中數據放入到request域對象中。
*
* @return
*/
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView modelAndView = new ModelAndView(SUCCESS);
// 將模型數據添加到ModelAndView中
modelAndView.addObject("time", new Date());
return modelAndView;
}
2.2Map及Model
/**
* 目標方法可以添加Map類型(實際上也可以是Model類型或ModelMap類型)的參數
* 開發的時候比較常用
* @param map
* @return
*/
@RequestMapping("testMap")
public String testMap(Map<String, Object> map){
map.put("names", Arrays.asList("tom","lily","james"));
return SUCCESS;
}
2.3 SessionAttributes
<span style="white-space:pre"> </span>/**
* 在@Controller上加上@SessionAttributes(value={"user"},types={String.class}), 只能放到類上,不能放在方法上
* 其中,value對應的鍵(user)會被既放到RequestScope中,也會被放到SessionScope中
* types則表示對應的對象(String)類型會被既放到RequestScope中,也會被放到SessionScope中
*/
@RequestMapping("/testSessionAttributes")
public String testSessionAttributes(Map<String, Object> map) {
map.put("user", new User("zhangsan", "123456", "[email protected]", 18));
map.put("school", "yuduzhongxue");
return SUCCESS;
}
2.4ModelAttribute
/**
* 有@ModelAttribute註解修飾的方法會在每個目標方法執行之前被SpringMVC調用
* @param id
* @param map
*/
@ModelAttribute
public void getUser(@RequestParam(value="id",required=false)Integer id,Map<String, Object> map) {
if(id!=null){
// 模擬從數據庫中獲取user對象
User user = new User(1, "Tom", "123456", "[email protected]", 88);
System.out.println("從數據庫中模擬獲取一個User對象 : "+user);
map.put("user", user);
}
}
<span style="white-space:pre"> </span>/**
* 產生500異常的話,將類上的SessionAttributes去掉即可--〉org.springframework.web.
* HttpSessionRequiredException: Session attribute 'user' required - not
* found in session
* 執行流程:
* 1.執行@ModelAttribute註解修飾的方法:從數據庫中取出對象,把對象放入到了Map中,鍵爲user
* 2.SpringMVC從Map中取出user對象,把表單獲取的參數賦值給對應的user對象屬性,
* 3.SpringMVC把上述對象傳入目標方法的參數。
*
* 注意:在@ModelAttribute修飾的方法中,放入到Map時的健需要與目標方法入參類型的第一個字母小寫的字符串一致。(user)
*/
@RequestMapping("/testModelAttributes")
public String testModelAttributes(User user) {
System.out.println("test testModelAttributes: " + user);
return SUCCESS;
}
HTML代碼
<!--
模擬修改操作
1.原始數據爲:1,tom,123456,[email protected],12
2.密碼不能修改
3.表單回顯,模擬操作直接在表單填寫對應的屬性值
-->
<form action="/first/testModelAttributes" method="post">
<input name="id" value="1" type="hidden" /><br>
userName:<input name="username" value="tom" type="text" /><br>
<!-- password:<input name="password" value="123456" type="password" /><br>
email:<input name="email" value="[email protected]" type="text" /><br> -->
age:<input name="age" value="18" type="text" /><br>
<input value="Submit" type="submit" /><br>
</form>
<br>
入參全過程
源碼分析