用包裝類型緣由:Controller方法參數中定義的是基本數據類型,可是從頁面提交過來的數據爲null或者」」的話,會出現數據轉換的異常php
Controller:java
@RequestMapping("/veiw")
public void test(Integer age) {
}
Form表單:markdown
<form action="/veiw" method="post">
<input name="age" value="5" type="text"/>
...
</form>
Controller中和form表單參數變量名保持一致,就能完成數據綁定,若是不一致能夠使用@RequestParam註解app
Model class:post
public class Chapter {
// 章節id
private Integer id;
// courseId
private Integer courseId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
}
Controller:this
@RequestMapping("/veiw")
public void test(Chapter chapter) {
}
form表單:spa
<form action="/veiw" method="post">
<input name="id" value="5" type="text"/>
<input name="courseId" value="5" type="text"/>
...
</form>
只要model中對象名與form表單的name一致便可code
Model class:orm
public Class Course{
// 課程Id
private Integer courseId;
// 課程名稱
private String title;
// 課程章節
private List<Chapter> chapterList;
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Chapter> getChapterList() {
return chapterList;
}
public void setChapterList(List<Chapter> chapterList) {
this.chapterList = chapterList;
}
}
Controller:對象
@RequestMapping("/veiw")
public void test(Course age) {
}
form表單:
<form action="/veiw" method="post">
<input name="chapter.id" value="5" type="text"/>
<input name="chapter.courseId" value="5" type="text"/>
...
</form>
使用「屬性名(對象類型的屬性).屬性名」來命名input的name
與上面的自定義複合屬性差異在於input中name的命名
public Class Course{
String id;
List<Chapter> chapter;
setter..
getter..
}
form(List、Set):
<form action="/veiw" method="post">
<input name="chapter[0].id" value="5" type="text"/>
<input name="chapter[0].courseId" value="5" type="text"/>
...
</form>
<form action="/veiw" method="post">
<input name="chapter["id"] value="5" type="text"/> <input name="chapter["courseId"] value="5" type="text"/>
...
</form>
@RequestParam:請求參數,用於請求Url?id=xxxxxx路徑後面的參數(即id)
當URL使用 Url?id=xxxxxx, 這時的id可經過 @RequestParam註解綁定它傳過來的值到方法的參數上。
@RequestMapping("/book")
public void findBook(@RequestParam String id) {
// implementation omitted
}