博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.数据绑定和表单标签库
阅读量:7290 次
发布时间:2019-06-30

本文共 10220 字,大约阅读时间需要 34 分钟。

1.有了数据绑定,类型总是为了String的HTTP请求参数,可用于填充不同的类型的对象属性

 

2.表单标签库

为了使用这些标签,必须在jsp页面开头处声明这个taglib指令

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

form,input,password,hidden,textarea,checkbox,checkboxs,radiobutton,radiobuttons

select,option,options,errors渲染元素

 

表单标签

1 
2 3 4 commandName它定义了模型属性的名称,其属性用于填充所生成的表单 5 6 @RequestMapping("/book_input") 7 public String inputBook(Model model){ 8 9 model.addAttribute("book",new Book()); 10 }11 12 如果没有Model属性,jsp会报错,无法找到commandName属性

 

input标签:

1 
2 path:属性,它将这个输入的字段绑定到book的一个属性,绑定到book的isbn属性 5

 

数据绑定范例:

 

 

1.Domain

1 package app05a.domain; 2  3 import java.io.Serializable; 4  5 public class Book implements Serializable { 6      7     private static final long serialVersionUID = 1520961851058396786L; 8     private long id; 9     private String isbn;10     private String title;11     private Category category;12     private String author;13     14     public Book() {15     }16     17     public Book(long id, String isbn, String title, 18             Category category, String author) {19         this.id = id;20         this.isbn = isbn;21         this.title = title;22         this.category = category;23         this.author = author;24     }25 26     public long getId() {27         return id;28     }29     public void setId(long id) {30         this.id = id;31     }32     public String getIsbn() {33         return isbn;34     }35     public void setIsbn(String isbn) {36         this.isbn = isbn;37     }38     public String getTitle() {39         return title;40     }41     public void setTitle(String title) {42         this.title = title;43     }44     public Category getCategory() {45         return category;46     }47     public void setCategory(Category category) {48         this.category = category;49     }50     public String getAuthor() {51         return author;52     }53     public void setAuthor(String author) {54         this.author = author;55     }56 }

 

1 package app05a.domain; 2  3 import java.io.Serializable; 4  5 public class Category implements Serializable { 6     private static final long serialVersionUID = 5658716793957904104L; 7     private int id; 8     private String name; 9     10     public Category() {11     }12     13     public Category(int id, String name) {14         this.id = id;15         this.name = name;16     }17     18     public int getId() {19         return id;20     }21     public void setId(int id) {22         this.id = id;23     }24     public String getName() {25         return name;26     }27     public void setName(String name) {28         this.name = name;29     }30 }

 

3.Controller

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

 

 

4.Service

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

 

 

5.

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

 

5.配置文件

1 
2
14 15
16
17 18
19
20
21 22
23
24
25
26 27

 

6.视图

1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 2  3  4  5 Book List 6  7  8  9 10 
11

Book List

12
">Add Book13
22
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29 30
Category Title ISBN Author  
${book.category.name} ${book.title} ${book.isbn} ${book.author} Edit
31
32 33

 

1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3  4  5  6 Add Book Form 7  8  9 10 11 
12
13
14
Add a book15

16 17

20

21

22 23

24

25

26 27

28

29

30 31

32

33 34

35 36 38

39
40
41
42 43

 

 

1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3  4  5  6 Edit Book Form 7  8  9 10 11 
12
13
14
Edit a book15
16

17 18

20

21

22 23

24

25

26 27

28

29

30 31

32

33 34

35 36 38

39
40
41
42 43

 

7.测试应用

http://localhost:8080/app05a/book_list

 

转载于:https://www.cnblogs.com/sharpest/p/5308969.html

你可能感兴趣的文章
《程序猿的生命周期》阅读有感
查看>>
重温排序算法
查看>>
Instrumentation 功能介绍(javaagent)
查看>>
Core J2EE Patterns - Data Access Object
查看>>
SpringCloud学习成长之路 六 cloud配置中心
查看>>
MyEclipse定位class文件
查看>>
STM32(HY-SRF05)超声波测距项目
查看>>
《practical Java》读书笔记
查看>>
数据库字段顺序的【坑】
查看>>
spring5新响应式框架-webflux实战
查看>>
软甲架构笔记 三
查看>>
STL training (uva上一些比较好的用来熟悉STL)
查看>>
[未完成]关于CSS的总结
查看>>
陈皓一起写Makefile 概述
查看>>
linux下安装启动rpc服务
查看>>
Software Testing, Lab 1
查看>>
World发布博客测试
查看>>
IIS 提高连接的并发数,和CPU的使用率。
查看>>
修改Sysvol复制方式
查看>>
python3.x中如何使用base64、base32、base16编码解码
查看>>