在公司coding的時候遇到的小問題,在網上瀏覽博文的時候發現了很好的解決辦法,決定自己另設情景,向大神學習,這里貼一下原地址
http://blog.csdn.net/qq_24505127/article/details/54236583
1.問題情景
項目中前端后臺的數據傳遞是必不可少的,比如說我們要在一張表單中提交對象,如果只是一個對象就就很好做,因為單獨的對象中是很難存在相同的參數名的,但是如果我們想要提交多個對象,這些對象的參數名如果在項目的設計階段可能會因為負責人溝通問題導致相同。
舉個栗子:
表單傳輸兩個對象:Student對象和Course對象,兩個對象中都有“id”這個參數,Student中“id”表示的是學號,Course中“id”表示的是課程號。兩個對象中都有“note”這個參數。在Student中“note“表示的是學生的備注,描述學生的。Course中”note“表示的是課程的備注,是描述課程的。這時候就會很煩,煩到掀桌(╯‵□′)╯︵┻━┻
再說主題之前我們先看看Struts2是如何解決這個問題的:
眾所周知,Struts2采用了OGNL表達式,可以使用Object.Prarm形式對表單進行精準綁定入參,比如傳student.id,后臺就可以區分并接收到是student的id。Struts2我很不熟(因為沒啥用),只知道這么一丟丟。
回到主題,我們主要是想看看在SpringMVC中我們怎么解決這個問題。
根據上面的情景,我們來搭建個問題,然后解決這個問題
先寫Student和Course兩個類,Student和Course里面都有id和note,參數名一樣,但是意義很不同!!!
后臺:
public class Student implements Serializable{
String id;
String note;
//get..set....
}
public class Course implements Serializable{
String id;
String note;
//set..get...
}
前端:
<form action="/test/test" method="get">
<input type="text" name="student.id" value="student_id">
<input type="text" name="student.name" value="student_name">
<input type="text" name="course.id" value="course_id">
<input type="text" name="course.name" value="course_name">
<input type="submit" value="提交">
</form>
2.解決問題
有請今天的主角——@InitBinder
有了它,我們就可以像Struts2一樣,前端傳Object.Prarm的形式了。當然在后臺我們需要做些風騷操作!
@InitBinder("student")
public void initBinderStudent(WebDataBinder binder){
binder.setFieldDefaultPrefix("student.");
}
@InitBinder("course")
public void initBinderCourse(WebDataBinder binder){
binder.setFieldDefaultPrefix("course.");
}
@InitBinder() 中間的value值,用于指定表單屬性或請求參數的名字,符合該名字的將使用此處的DataBinder。比如:student.id和student.note。student就得是中間的value值,這樣才能接收得到。而且student會填充進WebDataBinder,這里binder對象就是student了。
注意binder.setFieldDefaultPrefix("student."),這里的"."千萬別忘記了!!!
大家一定覺得這個操作太風騷了。先別著急用,這里說一下這個方法的缺陷:
首先,這個方法不能自定義路徑,比如我想寫:”/TianShenSchool/4thClass/{student.id}“ ,這就無法完成!
其次,如果給的是集合或者是數組,也無法使用。
當然如果真的出現上面兩種情況的話,建議大家還是乖乖改一改數據結構和系統設計吧ㄟ( ▔, ▔ )ㄏ
3.代碼
Student對象和Course對象:
public class Student implements Serializable{
String id;
String note;
//get..set....
}
public class Course implements Serializable{
String id;
String note;
//set..get...
}
html頁面:
<form action="/test/test" method="get">
<input type="text" name="student.id" value="student_id">
<input type="text" name="student.name" value="student_name">
<input type="text" name="course.id" value="course_id">
<input type="text" name="course.name" value="course_name">
<input type="submit" value="提交">
</form>
Controller:
@Controller
@RequestMapping("/classtest")
public class TestController {
// 綁定變量名字和屬性,參數封裝進類
@InitBinder("student")
public void initBinderUser(WebDataBinder binder) {
binder.setFieldDefaultPrefix("student.");
}
// 綁定變量名字和屬性,參數封裝進類
@InitBinder("course")
public void initBinderAddr(WebDataBinder binder) {
binder.setFieldDefaultPrefix("course.");
}
@RequestMapping("/methodtest")
@ResponseBody
public Map<String,Object> test(@ModelAttribute("student") Student student,@ModelAttribute("course") Course course){
Map<String,Object> map=new HashMap<String,Object>();
map.put("student", student);
map.put("course", course);
return map;
}
至此問題完美解決~