我的開發環境
框架: springmvc+spring+freemarker
開發工具: springsource-tool-suite-2.9.0
JDK版本: 1.6.0_29
tomcat版本:apache-tomcat-7.0.26
前置文章-SpirngMVC配置入門 http://www.cnblogs.com/sunang/p/3419544.html
Spring整合Freemarker http://www.cnblogs.com/sunang/p/3419676.html
SpringMVC常用注解實例詳解:@Controller,@RequestMapping,@RequestParam,@PathVariable http://www.cnblogs.com/sunang/p/3421707.html
@ModelAttribute用于綁定控制器接收的實體對象,本文以一個表單提交的例子來演示下這個註釋的用法:
首先,寫一個表單頁面register.ftl,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <table> <form name="register" action="register.htm" method="post"> <tr> <td>姓名</td> <td><input name="userName" type="text"></input></td> </tr> <tr> <td>年齡</td> <td><input name="age" type="text"></input></td> </tr> <tr> <td><input type="submit" value="提交"></input></td> </tr> </form> <table> </body> </html>
還有一個註冊成功的頁面registerSuccess.ftl:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>註冊成功!用戶名:${userNameGot} 年齡:${ageGot}</h2> </body> </html>
接下來,我們要新建一個VO(value object)文件用來把表單信息綁定到vo對象,在src/main/java目錄下新建包www.asuan.com.vo,在包里新建UserInfo.java,代碼如下:
package www.asuan.www.vo; public class UserInfo { private String userName; private int age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
接下是controller類,代碼如下:
package www.asuan.com.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import www.asuan.www.vo.UserInfo; @Controller @RequestMapping("/learnMVC") public class LearnMVCController { //使用該索引跳轉到註冊頁面 @RequestMapping("/goRegisterPage") public String goRegisterPage() { return "register.ftl"; } //使用該索引進行註冊操作并返回註冊成功頁面 @RequestMapping("/register") public String register(@ModelAttribute(value = "register") UserInfo userInfo, Model model) { //通過@ModelAttribute,我們把表單信息綁定到了userInfo對象裏面,可以直接使用 String userName = userInfo.getUserName(); int age = userInfo.getAge(); model.addAttribute("userNameGot", userName); model.addAttribute("ageGot", age); return "registerSuccess.ftl"; } }
部署運行工程,在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/goRegisterPage.htm 訪問表單填寫頁面并填寫測試信息,運行結果如下:
點擊提交,表單的action配置了要訪問的controller路徑register.htm,所以控制器會執行register()方法,我們把該方法單獨列出分析:
@RequestMapping("/register") public String register(@ModelAttribute(value = "register") UserInfo userInfo, Model model) { //通過@ModelAttribute,我們把表單信息綁定到了userInfo對象裏面,可以直接使用 String userName = userInfo.getUserName(); int age = userInfo.getAge(); model.addAttribute("userNameGot", userName); model.addAttribute("ageGot", age); return "registerSuccess.ftl"; }
控制器會掃描與@ModelAttribute的value值相同的實體對象,這裡value = "register",而提交的表單的name="register"兩者相同,所以提交的表單信息被綁定到了@ModelAttribute後面聲明的UserInfo對象中。
表單中的各個字段和實體類對象的各個字段會按照字段名一一匹配,比如表單userName匹配UserInfo對象中的userName。在上例中表單的兩個字段被綁定到了userInfo對象的兩個字段中,我們在進行業務處理的時候可以直接使用,通過這種方法我們就得到了表單的信息。
運行結果:
complete!