19.3 数据绑定范例

19.3 数据绑定范例

在表单标签库中利用标签进行数据绑定的例子,见app19a 应用程序。这个范例围绕着domainBook进行。这个类中有几个属性,包括一个类型为Categorycategory属性。Categoryidname两个属性。
这个应用程序允许列出书目、添加新书,以及编辑书目。

19.3.1 目录结构

下面是app19a的目录结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
E:\workspace_web\app19a
├─pom.xml
├─src
│ ├─main
│ │ ├─java
│ │ │ └─app19a
│ │ │ ├─controller
│ │ │ │ └─BookController.java
│ │ │ ├─domain
│ │ │ │ ├─Book.java
│ │ │ │ └─Category.java
│ │ │ └─service
│ │ │ ├─BookService.java
│ │ │ └─BookServiceImpl.java
│ │ └─resources
│ └─test
│ └─java
└─WebContent
├─css
│ └─main.css
├─META-INF
│ └─MANIFEST.MF
└─WEB-INF
├─config
│ └─springmvc-config.xml
├─jsp
│ ├─BookAddForm.jsp
│ ├─BookEditForm.jsp
│ └─BookList.jsp
├─lib
└─web.xml

19.3.2Domain类

Book类和Category类是这个应用程序中的domain类,它们分别如清单19.1和清单19.2所示。

清单19.1Book类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package app19a.domain;
import java.io.Serializable;
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;
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getIsbn()
{
return isbn;
}
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public Category getCategory()
{
return category;
}
public void setCategory(Category category)
{
this.category = category;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
}

Category类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package app19a.domain;
import java.io.Serializable;
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;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}

19.3.3Controller类

下面的范例为Book提供了一个controllerBookController类。它允许用户创建新书目、更新书的详细信息,并在系统中列出所有书目。清单19.3中展示了BookController类。

BookController类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package app19a.controller;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import app19a.domain.Book;
import app19a.domain.Category;
import app19a.service.BookService;
@Controller
public class BookController
{
@Autowired
private BookService bookService;
private static final Log logger = LogFactory
.getLog(BookController.class);
@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_edit/{id}")
public String editBook(Model model,@PathVariable
long id)
{
List<Category> categories =
bookService.getAllCategories();
model.addAttribute("categories", categories);
Book book = bookService.get(id);
model.addAttribute("book", book);
return "BookEditForm";
}
@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";
}
@RequestMapping(value = "/book_update")
public String updateBook(@ModelAttribute
Book book)
{
Category category = bookService
.getCategory(book.getCategory().getId());
book.setCategory(category);
bookService.update(book);
return "redirect:/book_list";
}
@RequestMapping(value = "/book_list")
public String listBooks(Model model)
{
logger.info("book_list");
List<Book> books = bookService.getAllBooks();
model.addAttribute("books", books);
return "BookList";
}
}

BookController依赖BookService进行一些后台处理。@Autowired注解用于给BookController注入一个BookService实现:

1
2
@Autowired
private BookService bookService;

19.3.4 Service类

清单19.4和清单19.5分别展示了BookService接口和BookServiceImpl类。顾名思义,BookServiceImpl就是实现BookService

BookService接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package app19a.service;
import java.util.List;
import app19a.domain.Book;
import app19a.domain.Category;
public interface BookService
{
List<Category> getAllCategories();
Category getCategory(int id);
List<Book> getAllBooks();
Book save(Book book);
Book update(Book book);
Book get(long id);
long getNextId();
}

BookServiceImpl类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package app19a.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import app19a.domain.Book;
import app19a.domain.Category;
@Service
public class BookServiceImpl
implements
BookService
{
/*
* this implementation is not thread-safe
*/
private List<Category> categories;
private List<Book> books;
public BookServiceImpl()
{
categories = new ArrayList<Category>();
Category category1 = new Category(1, "Computing");
Category category2 = new Category(2, "Travel");
Category category3 = new Category(3, "Health");
categories.add(category1);
categories.add(category2);
categories.add(category3);
books = new ArrayList<Book>();
books.add(new Book(1L, "9780980839623",
"Servlet & JSP: A Tutorial",
category1, "Budi Kurniawan"));
books.add(new Book(2L, "9780980839630",
"C#: A Beginner's Tutorial",
category1, "Jayden Ky"));
}
@Override
public List<Category> getAllCategories()
{
return categories;
}
@Override
public Category getCategory(int id)
{
for(Category category: categories)
{
if(id == category.getId())
{
return category;
}
}
return null;
}
@Override
public List<Book> getAllBooks()
{
return books;
}
@Override
public Book save(Book book)
{
book.setId(getNextId());
books.add(book);
return book;
}
@Override
public Book get(long id)
{
for(Book book: books)
{
if(id == book.getId())
{
return book;
}
}
return null;
}
@Override
public Book update(Book book)
{
int bookCount = books.size();
for(int i = 0;i < bookCount;i++)
{
Book savedBook = books.get(i);
if(savedBook.getId() == book.getId())
{
books.set(i, book);
return book;
}
}
return book;
}
@Override
public long getNextId()
{
// needs to be locked
long id = 0L;
for(Book book: books)
{
long bookId = book.getId();
if(bookId > id)
{
id = bookId;
}
}
return id + 1;
}
}

BookServiceImpl类中包含了一个Book对象的List和一个Category对象的List。这两个List都是在实例化类时生成的。这个类中还包含了获取所有书目、获取单个书目以及添加和更新书目的方法。

19.3.5 配置文件

清单19.6展示了app19a中的Spring MVC配置文件。

SpringMVC配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫描控制器 -->
<context:component-scan
base-package="app19a.controller" />
<!-- 扫描服务类 -->
<context:component-scan
base-package="app19a.service" />
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/*.html" location="/" />
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

component-scan bean使得app19a.controller包和app19a.service包得以扫描。

19.3.6 视图

app19a中使用的3个JSP页面如清单19.7、清单19.8和清单19.9所示。BookAddForm.jspBookEditForm.jsp页面中使用的是来自表单标签库的标签。

清单19.7 BookList.jsp页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<title>Book List</title>
<style type="text/css">
@import url("<c:url value="/css/main.css"/>");
</style>
</head>
<body>
<div id="global">
<h1>Book List</h1>
<a href="<c:url value="/book_input"/>">Add Book</a>
<table>
<tr>
<th>Category</th>
<th>Title</th>
<th>ISBN</th>
<th>Author</th>
<th>&nbsp;</th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.category.name}</td>
<td>${book.title}</td>
<td>${book.isbn}</td>
<td>${book.author}</td>
<td><a href="book_edit/${book.id}">Edit</a></td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>

清单19.8 BookAddForm.jsp页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<title>Add Book Form</title>
<style type="text/css">
@import url("<c:url value="/css/main.css"/>");
</style>
</head>
<body>
<div id="global">
<!-- SpringMVC表单标签 -->
<form:form commandName="book" action="book_save" method="post">
<fieldset>
<legend>Add a book</legend>
<p>
<label for="category">Category: </label>
<form:select id="category" path="category.id"
items="${categories}" itemLabel="name"
itemValue="id" />
</p>
<p>
<label for="title">Title: </label>
<form:input id="title" path="title" />
</p>
<p>
<label for="author">Author: </label>
<form:input id="author" path="author" />
</p>
<p>
<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>

清单19.9 BookEditForm.jsp页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<title>Edit Book Form</title>
<style type="text/css">
@import url("<c:url value="/css/main.css"/>");
</style>
</head>
<body>
<div id="global">
<form:form commandName="book" action="/book_update"
method="post">
<fieldset>
<legend>Edit a book</legend>
<form:hidden path="id" />
<p>
<label for="category">Category: </label>
<form:select id="category" path="category.id"
items="${categories}" itemLabel="name"
itemValue="id" />
</p>
<p>
<label for="title">Title: </label>
<form:input id="title" path="title" />
</p>
<p>
<label for="author">Author: </label>
<form:input id="author" path="author" />
</p>
<p>
<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="Update Book">
</p>
</fieldset>
</form:form>
</div>
</body>
</html>

19.3.7 测试应用

要想测试这个应用程序范例,请打开以下网页:
http://localhost:8080/app19a/book_list
图19.2所示为第一次启动这个应用程序时显示的书目列表。

单击Add Book链接添加书目,或者单击书籍详情右侧的Edit链接来编辑书目。
图19.3所示为Add Book表单。图19.4所示为EditBook表单。