springmvc中可以使用表單標簽庫,支持數據綁定,用來將用戶輸入綁定到領域模型。
例子來源《Servlet、JSP和SpringMVC學習指南》
項目代碼
關鍵代碼及說明
bean對象類代碼
public class Book implements Serializable {
private static final long serialVersionUID = 1520961851058396786L;
private long id;
private String isbn;
private String title;
private Category category;
private String author;
public Book() {
}
public Book(long id, String isbn, String title, Category category, String author) {
this.id = id;
this.isbn = isbn;
this.title = title;
this.category = category;
this.author = author;
}
// get and set methods not shown
}
public class Category implements Serializable {
private static final long serialVersionUID = 5658716793957904104L;
private int id;
private String name;
public Category() {
}
public Category(int id, String name) {
this.id = id;
this.name = name;
}
// get and set methods not shown
}
Controller中的代碼,需要在Model中添加綁定的對象
@RequestMapping(value = "/book_input")
public String inputBook(Model model) {
List<Category> categories = bookService.getAllCategories();
model.addAttribute("categories", categories);
model.addAttribute("book", new Book());
return "BookAddForm";
}
@RequestMapping(value = "/book_save")
public String saveBook(@ModelAttribute Book book) {
Category category =
bookService.getCategory(book.getCategory().getId());
book.setCategory(category);
bookService.save(book);
return "redirect:/book_list";
}
jsp中代碼,使用表單標簽
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Add Book Form</title>
</head>
<body>
<div id="global">
<!--commandName定義了模型屬性的名稱,其中包含了一個backing object,其屬性將用於填充所生成的表單。如果該屬性存在,則必須在返回包含該表單的視圖的請求處理方法中添加相應的模型屬性-->
<form:form commandName="book" action="book_save" method="post">
<fieldset>
<legend>Add a book</legend>
<p>
<label for="category">Category: </label>
<!--標簽也可以綁定到嵌套對象的屬-->
<!--綁定book.category.id-->
<!--items指定select的可選值來自model中的categories集合-->
<!--itemLabel指定集合中對象的某個屬性為每個input元素提供label-->
<!--itemValue指定集合中對象的某個屬性為每個input元素提供值-->
<form:select path="category.id" items="${categories}" itemLabel="name" itemValue="id"/>
</p>
<p>
<!-- path指定輸入字段綁定到formbacking object的一個屬性-->
<!--綁定book.title-->
<label for="title">Title: </label>
<form:input id="title" path="title"/>
</p>
<p>
<!--綁定book.author-->
<label for="author">Author: </label>
<form:input id="author" path="author"/>
</p>
<p>
<!--綁定book.isbn-->
<label for="isbn">ISBN: </label>
<form:input id="isbn" path="isbn"/>
</p>
<p id="buttons">
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5"
value="Add Book">
</p>
</fieldset>
</form:form>
</div>
</body>
</html>
表單標簽庫中包含了可以用在JSP頁面中渲染HTML元素的標簽。為了使用這些標簽,必須在JSP頁面的開頭處聲明這個taglib指令:<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>,form標簽用於渲染HTML表單。form標簽必須利用渲染表單輸入字段的其他任意標簽。commandName屬性或許是其中最重要的屬性,因為它定義了模型屬性的名稱,其中包含了一個backingobject,其屬性將用於填充所生成的表單。如果該屬性存在,則必須在返回包含該表單的視圖的請求處理方法中添加相應的模型屬性。在該例子中,book_input請求的處理方法中,會在Model中添加要綁定的對象,然后在BookAddForm.jsp中通過commandName綁定該對象。其他輸入標簽中的path指定了要與其綁定的對象屬性。
