开发运行环境:NetBeans7.3.1。
这里使用的是JSF 2.2的新特性:Faces Flow,关于JSF 2.2的介绍,请参考《JavaEE7 十大新特性》。
预订电影票功能包括一系列页面:
(1)显示电影列表。
(2)显示某个电影的观影时间表。
(3)确认购买。
(4)显示购买详细信息。
这里使用Faces Flow,把这些页面放到一个Flow中,这样可以达到页面功能模块化的目的。
1. Booking.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.booking;
import java.util.List;
import java.util.StringTokenizer;
import javax.faces.flow.FlowScoped;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import org.glassfish.movieplex7.entities.Movie;
import org.glassfish.movieplex7.entities.ShowTiming;
/**
*
* @author pmma
*/
@Named
@FlowScoped("booking")
public class Booking {
int movieId;
String startTime;
int startTimeId;
@PersistenceContext
EntityManager em;
public int getMovieId() {
return movieId;
}
public void setMovieId(int movieId) {
this.movieId = movieId;
}
public String getMovieName() {
try {
return em.createNamedQuery("Movie.findById",
Movie.class).setParameter("id", movieId).getSingleResult().getName();
} catch (NoResultException e) {
return "";
}
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
StringTokenizer tokens = new StringTokenizer(startTime, ",");
startTimeId = Integer.parseInt(tokens.nextToken());
this.startTime = tokens.nextToken();
}
public int getStartTimeId() {
return startTimeId;
}
public String getTheater() {
// for a movie and show
try {
// Always return the first theater
List list =
em.createNamedQuery("ShowTiming.findByMovieAndTimingId",
ShowTiming.class)
.setParameter("movieId", movieId)
.setParameter("timingId", startTimeId)
.getResultList();
if (list.isEmpty()) {
return "none";
}
return list
.get(0)
.getTheaterId()
.getId().toString();
} catch (NoResultException e) {
return "none";
}
}
}
说明:
(1)@FlowScoped("booking")表明该类的生命周期范围是Flow Scope,"booking"对应Flow定义的id值。
2. booking-flow.xml
在booking目录下,有booking.xhtml,showtimes.xhtml,confirm.xhtml,print.xhtml。
现在在booking目录下,创建booking-flow.xml。
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<flow-definition id="booking">
<flow-return id="goHome">
<from-outcome>/index</from-outcome>
</flow-return>
</flow-definition>
</faces-config>
Project 下载:9. movieplex7(JSF).7z
没有评论:
发表评论