主要是針對Member 作所謂的 Add, Delete, Inquiry, Update. 並將所有動作整合到同一個頁面。
1. Controller
新增BasicMemberOperation.java 在對應的目錄下,這個是要當Controller用的,網頁的每一個動作都對應到Controller裡面的某個method。 最後透過Index呼叫PlayFramework作render。
N:\PlayFramework\play-1.1\members\app\controllers\BasicMemberOperation.java
package controllers; import java.util.List; import models.Member; import models.MemberCard; import play.data.validation.Required; import play.mvc.Controller; public class BasicMemberOperation extends Controller { public static void index() { List2.Viewmembers = Member.getall(); // get all member data render(members); } //將選取的資料傳回表單 public static void getDataForEdit(Long id) { Member member = Member.findById(id); notFoundIfNull(member); flash.put("id", member.id); flash.put("firstName", member.firstName); flash.put("lastName", member.lastName); flash.put("email", member.email); index(); } //delete member public static void delete(Long id) { Member member = Member.findById(id); notFoundIfNull(member); member.delete(); flash.success("The member %s has been deleted", member.firstName ); index(); } //add or edit member public static void addOrEdit(long id,@Required String firstName,@Required String lastName , @Required String email) { if(validation.hasErrors()) { params.flash(); // add http parameters to the flash scope validation.keep(); // keep the errors for the next request index(); } if (id == 0){ //no id, add member new Member(firstName,lastName,0,email).insert(); }else{ // have id, edit member Member member = Member.findById(id); member.firstName = firstName; member.lastName = lastName; member.email = email; member.update(); } index(); } //產生測試資料 public static void genData() { int count = 10; for (int i=1;i<=count;i++){ Member theMember = new Member("FirstName"+i,"lastName"+i,0,"email"+i); theMember.insert(); for (int j=1;j<=20;j++){ MemberCard theCard = new MemberCard("NO:"+i+j,theMember); theCard.startDate = new java.util.Date(); theCard.insert(); System.out.println("NO:"+i+j); } } flash.success("The member has been deleted, row=%s", count); index(); } //清空測試資料 public static void removeAllData() { int count = Member.all().delete(); for (Member m: Member.all().fetch()) m.delete(); flash.success("The member has been deleted, row=%s", count); index(); } }
N:\PlayFramework\play-1.1\members\app\views\BasicMemberOperation\index.html
#{extends 'main.html' /} #{set title:'Home' /} <h2> #{a @BasicMemberOperation.genData()}<b>Create Test Data</b>#{/a} <BR> #{a @BasicMemberOperation.removeAllData()}<b>Remove all Data</b>#{/a} </h2> <hr> #{form @BasicMemberOperation.addOrEdit()} <p> <input type="hidden" name="id" id="id" size="60" value="${flash.id}"/> Member First Name <input type="text" name="firstName" id="firstName" size="20" value="${flash.firstName}"/> <span class="error">#{error 'firstName' /}</span> <BR> Member Last Name <input type="text" name="lastName" id="lastName" size="20" value="${flash.lastName}"/> <span class="error">#{error 'lastName' /}</span> <BR> Member Email <input type="text" name="email" id="email" size="40" value="${flash.email}"/> <span class="error">#{error 'email' /}</span> </p> <p> #{if flash.id == null} <input type="submit" value="ADD Data"> #{/if} #{else} <input type="submit" value="SAVE"> #{/else} </p> #{/form} <hr> <h2>All Member List</h2> #{if members.size() > 0} <table BORDER=1> <tr > <td><b>Member First Name</b></td> <td><b>Member Last Name</b></td> <td><b>Member Email</b></td> <td><b>Action</b></td> </tr> #{list items:members, as:'members'} <tr> <td>${members.firstName} </td> <td>${members.lastName}</td> <td>${members.email}</td> <td> #{a @BasicMemberOperation.getDataForEdit(members.id)}Edit#{/a} #{a @BasicMemberOperation.delete(members.id)}Delete#{/a} </td> </tr> #{/list} </table> #{/if} #{else} <div class="empty"> There is currently nothing to show here. </div> #{/else}3. Model
Model 的部份請參照第一篇,將資料物件相關的檔案放到N:\PlayFramework\play-1.1\members\app\models下
4. 執行專案
執行以下命令 N:\PlayFramework\play-1.1>play run members 之後直接用瀏覽器開啟 http://127.0.0.1:9000/basicmemberoperation/index 便可以進行操作