實現方法

一、實現create方法
1.在控制器中創建一個表單:
import javax.inject.Inject;
public class BooksController extends Controller {
@Inject
FormFactory formFactory;}
2.在create中添加:
public Result create(){
Form<Book> bookForm = formFactory.form(Book.class)};
3.在views的books包中創建名爲create的Play 2 Template,其內容爲:
@(bookFrom:Form[Book])
@import helper._
<html>
<head>
<title>Create Books</title>
</head>
<body>
<h1>Create Books</h1>
@helper.form(action = routes.BooksController.save()){
@helper.CSRF.formField
@helper.inputText(bookFrom("id"))
@helper.inputText(bookFrom("title"))
@helper.inputText(bookFrom("price"))
@helper.inputText(bookFrom("author"))

<button class="btn btn-success" type="submit">
<i class="glyphicon glyphicon-plus"></i>
Create Book
</button>
}
</body>
</html>
4.在create中添加返回值
return ok(create.render(bookForm));
5.在model的book下創建模型:
public Book(){}
6.測試方法localhost:9000/books/create將看到如下界面:
Create Books
id
title
price
author
Create Book

二、實現save方法

1.在save方法中添加如下方法:
建表、獲取表中書籍,添加書籍
public Result save(){
Form<Book> bookForm = formFactory.form(Book.class).bindFromRequest();
Book book = bookForm.get();
Book.add(book);
return redirect(routes.BooksController.index());
}


三、實現edit的方法

1.在控制器中找到edit做如下處理:
public Result edit(Integer id){
//先找書id
Book book = Book.findById(id);
//判斷要找的書是否存在
if(book == null){
return notFound("Book Not Found");
}
//創建書表單填充書籍
Form<Book> bookForm = formFactory.form(Book.class).fill(book);
2.在views 的books下創建名爲edit的Play 2 Template
@(bookFrom:Form[Book])
@import helper._
<html>
<head>
<title>Edit Books</title>
</head>
<body>
<h1>Edit Books</h1>
@helper.form(action = routes.BooksController.update()){
@helper.inputText(bookFrom("id"))
@helper.inputText(bookFrom("title"))
@helper.inputText(bookFrom("price"))
@helper.inputText(bookFrom("author"))

<input type = "submit" value="Edit Book">
}
</body>
</html>
3.在edit中添加返回值
return ok(edit.render(bookForm));
4.localhost:9000/books/edit/3測試效果
Edit Books
id 3
title c
price 22
author wangshiyu
Edit Book
若找不到數據庫中的書籍則返回空


四、實現update方法

1.在控制器中找到update做如下修改:
public Result update(){
// Form<Book> bookForm = formFactory.form(Book.class).bindFromRequest();
// Book book = bookForm.get();
Book book = formFactory.form(Book.class).bindFromRequest().get();
Book oldBook = Book.findById(book.id);

if(oldBook == null){
return notFound("Book Not Found");
}

oldBook.title = book.title;
oldBook.author = book.author;
oldBook.price = book.price;

return redirect(routes.BooksController.index());
}


五、實現show方法

1.在views的books下創建show視圖
@(book:Book)
<html>
<head>
<title>@book.title</title>
</head>
<body>
<h2>@book.title</h2>
<p>Price : @book.price $</p>
<p>Price : @book.author </p>
</body>
</html>
2.在方法show中做如下修改:
public Result show(Integer id){
Book book = Book.findById(id);
if(book == null){
return notFound("Book Not Found");
}
return ok(show.render(book));
}
3.在views的books下創建index視圖做點小修改
@(books:Set[Book])
<html>
<head>
<title>All Books</title>
</head>
<body>
<h1>All Books</h1>
@for(book <- books){
<a href="@routes.BooksController.show(book.id)">@book.title</a>//修改了此行
<P>price : @book.price</P>
<p>author: @book.author</p>
}
</body>
</html>
4.輸入localhost:9000/books/1測試效果
C++
Price : 20 $
Price : leiyuxing


六、實現delete方法

1.在控制器中找到該方法:
public Result destroy(Integer id){
Book book = Book.findById(id);
if(book == null){
return notFound("Book Not Found");
}
Book.remove(book);
return redirect(routes.BooksController.index());
}
2.在show視圖中做點小修改--添加如下:
<a href="@routes.BooksController.edit(book.id)">Edit</a>
<a href="@routes.BooksController.destory(book.id)">Delete</a>


解決顯示不了問題:
在edit 和create視圖中添加此行代碼:
@helper.CSRF.formField


發佈了132 篇原創文章 · 獲贊 35 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章