我正在练习 JSF+JPA 并遇到以下异常:
19/03/2013 00:04:18 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/K19-Futebol] threw exception [/times.xhtml @11,60 <ui:include src="formulario-novo-time.xhtml"> Invalid path : formulario-novo-time.xhtml] with root cause
javax.faces.view.facelets.TagAttributeException: /times.xhtml @11,60 <ui:include src="formulario-novo-time.xhtml"> Invalid path : formulario-novo-time.xhtml
[...]
at filters.JPAFilter.doFilter(JPAFilter.java:42)
[...]
当我关注 BalusC 链接(如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML?)时,我认为问题与 Facelets 无关,但这个过滤器:
@WebFilter(servletNames = {"Faces Servlet"})
public class JPAFilter implements Filter {
private EntityManagerFactory factory;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.factory = Persistence.createEntityManagerFactory("K19-Futebol-PU");
}
@Override
public void destroy() {
this.factory.close();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// CHEGADA
EntityManager manager = this.factory.createEntityManager();
request.setAttribute("EntityManager", manager);
manager.getTransaction().begin();
// CHEGADA
// FACES SERVLET
chain.doFilter(request, response);
// FACES SERVLET
// SAÍDA
try {
manager.getTransaction().commit();
} catch (Exception e) {
manager.getTransaction().rollback();
} finally {
manager.close();
}
// SAÍDA
}
}
我还询问您的意见,如果以这种方式使用过滤器是一种好的做法,我的意思是,对模型/持久层使用过滤器。
其他需要澄清的类:
@Entity
public class Time implements Serializable { //in portuguese 'Time' means Team (e.g.:football Team)
@Id
@GeneratedValue
private Long id;
private String nome;
private String tecnico;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTecnico() {
return tecnico;
}
public void setTecnico(String tecnico) {
this.tecnico = tecnico;
}
}
托管bean:
@ManagedBean
public class TimeBean {
public Time getTime() {
return time;
}
public void setTime(Time time) {
this.time = time;
}
public void setTimes(List<Time> times) {
this.times = times;
}
private Time time = new Time();
private List<Time> times;
public void adiciona() {
EntityManager manager = this.getManager();
TimeRepository repository = new TimeRepository(manager);
if (this.time.getId() == null) {
repository.adiciona(this.time);
} else {
repository.atualiza(this.time);
}
this.time = new Time();
this.times = null;
}
public void preparaAlteracao() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
Long id = Long.parseLong(params.get("id"));
EntityManager manager = this.getManager();
TimeRepository repository = new TimeRepository(manager);
this.time = repository.procura(id);
}
public void remove() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
Long id = Long.parseLong(params.get("id"));
EntityManager manager = this.getManager();
TimeRepository repository = new TimeRepository(manager);
repository.remove(id);
this.times = null;
}
public List<Time> getTimes() {
if (this.times == null) {
EntityManager manager = this.getManager();
TimeRepository repository = new TimeRepository(manager);
this.times = repository.getLista();
}
return this.times;
}
private EntityManager getManager() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
return (EntityManager) request.getAttribute(" EntityManager ");
}
}
“存储库”类:
public class TimeRepository {
private EntityManager manager;
public TimeRepository(EntityManager manager) {
this.manager = manager;
}
public void adiciona(Time time) {
this.manager.persist(time);
}
@SuppressWarnings("unchecked")
public void remove(Long id) {
Time time = this.procura(id);
Query query = this.manager.createQuery("select x from Jogador x");
List<Jogador> jogadores = query.getResultList();
for (Jogador jogador : jogadores) {
jogador.setTime(null);
}
this.manager.remove(time);
}
public Time atualiza(Time time) {
return this.manager.merge(time);
}
public Time procura(Long id) {
return this.manager.find(Time.class, id);
}
@SuppressWarnings("unchecked")
public List<Time> getLista() {
Query query = this.manager.createQuery("select x from Time x");
return query.getResultList();
}
}
我怎样才能解决这个问题?