日期:2014-05-20 浏览次数:21290 次

@Entity
public class Hotel extends Model {
@Required
public String name;
public String address;
public String city;
…..省略部分字段
@Column(precision=6, scale=2)
public BigDecimal price;
public String toString() {
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")";
}
}
@Entity
public class Booking extends Model {
@Required
@ManyToOne
public User user;
@Required
@ManyToOne
public Hotel hotel;
@Required
@Temporal(TemporalType.DATE)
public Date checkinDate;
@Required
@Temporal(TemporalType.DATE)
public Date checkoutDate;
@Required(message="Credit card number is required")
@Match(value="^\\d{16}$", message="Credit card number must be numeric and 16 digits long")
public String creditCard;
@Required(message="Credit card name is required")
public String creditCardName;
public int creditCardExpiryMonth;
public int creditCardExpiryYear;
public boolean smoking;
public int beds;
public Booking(Hotel hotel, User user) {
this.hotel = hotel;
this.user = user;
}
public BigDecimal getTotal() {
return hotel.price.multiply( new BigDecimal( getNights() ) );
}
public int getNights() {
return (int) ( checkoutDate.getTime() - checkinDate.getTime() ) / 1000 / 60 / 60 / 24;
}
public String getDescription() {
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
return hotel==null ? null : hotel.name +
", " + df.format( checkinDate ) +
" to " + df.format( checkoutDate );
}
public String toString() {
return "Booking(" + user + ","+ hotel + ")";
}
}
public class Hotels extends Application {
@Before//拦截器
static void checkUser() {
if(connected() == null) {
flash.error("Please log in first");
Application.index();
}
}
public static void index() {
List<Booking> bookings = Booking.find("byUser", connected()).fetch();//这句是不是更加面向对象?
render(bookings);
}
public static void list(String search, Integer size, Integer page) {
List<Hotel> hotels = null;
page = page != null ? page : 1;
if(search.trim().length() == 0) {
//分页的代码是不是很简单?链式调用更加方便
hotels = Hotel.all().fetch(page, size);
} else {
search = search.toLowerCase();
hotels = Hotel.find("lower(name) like ? OR lower(city) like ?", "%"+search+"%", "%"+search+"%").fetch(page, size);
}
render(hotels, search, size, page);
}
public static void book(Long id) {
Hotel hotel = Hotel.findById(id);
render(hotel);
}
public static void confirmBooking(Long id, Booking booking) {
Hotel hotel = Hotel.findById(id);
booking.hotel = hotel;
booking.user = connected();
validation.valid(booking);
// Errors or revise
if(validation.hasErrors() || params.get("revise") != null) {
render("@book", hotel, booking);
}
// Confirm
if(params.get("confirm") != null) {
booking.save();
flash.success("Thank you, %s, your confimation number for %s is %s", connected().name, hotel.name, booking.id);
index();
}
// Display booking
render(hotel, booking);
}
public static void saveSettings(String password, String verifyPassword) {
User connected = connected();
connected.password = password;
validation.valid(connected);
validation.required(verifyPassword);
validation.equals(verifyPassword, password).message("Your password doesn't match");
if(validation.hasErrors()) {
render("@settings", connected, verifyPassword);
}
connected.save();
flash.success("Password updated");
index();
}
}
public static void save(Picture picture,File pic){
File uploadFile=new File(Play.applicationPath.getAbsoluteFile()+”/public/uploads”);
play.libs.Files.copy(pic,uploadFile);
picture.url =path;
picture.save();
QZ_Admin.pictures();
}
#{extends 'main.html' /}///在views文件夹下面编写main.html一般为网站所有页面的公共部分,比如header和footer
#{set title:'Search' /}//为每一个页面设置title 在Main.html有变量title
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City, State</th>
<th>Check in</th>
<th>Check out</th>
<th>Confirmation number</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#{list bookings, as:'booking'} //遍历
<tr>
<td>${booking.hotel.name}</td>
<td>${booking.hotel.address}</td>
<td>${booking.hotel.city},${booking.hotel.state}, ${booking.hotel.country}</td>
<td>${booking.checkinDate.format('yyyy-MM-dd')}</td>
<td>${booking.checkoutDate.format('yyyy-MM-dd')}</td>
<td>${booking.id}</td>
<td>
#{a @cancelBooking(booking.id)}Cancel#{/a}
</td>
</tr>
#{/list}
</tbody>
</table>
2、 public static void showProduct(String id) {
3、 Product product = Cache.get("product_" + id, Product.class);
4、 if(product == null) {
5、 product = Product.findById(id);
6、 Cache.set("product_" + id, product, "30mn");
7、 }
8、 render(product);
9、 }
3、 @Every("1h")
4、 public class Bootstrap extends Job {
5、
6、 public void doJob() {
7、 List<User> newUsers = User.find("newAccount = true").fetch();
8、 for(User user : newUsers) {
9、 Notifier.sayWelcome(user);
10、 }
11、 }
12、
13、 }
4、 Template t =TemplateLoader.load("UserCenter/mailTemplate.html");//邮件模板
5、 Scope.RenderArgs templateBinding = Scope.RenderArgs.current();
6、 templateBinding.put("url","http;//url"));
7、 String result =t.render(templateBinding.data);
8、 Mail.send("from@163.com", "to@163.com", "",result,"text/html")