3

我正在练习 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();
    }
}

我怎样才能解决这个问题?

4

1 回答 1

4

javax.faces.view.facelets.TagAttributeException:/times.xhtml @11,60<ui:include src="formulario-novo-time.xhtml">无效路径:formulario-novo-time.xhtml

此问题不是由过滤器引起的。它恰好在调用堆栈中,因为它是在 faces servlet 之前调用的。

抛出. FaceletContext#includeFacelet()_ IOException不幸的是,这IOException反过来没有被包装为根本原因,它只是登录FINE级别。您可能需要打开FINE(或ALL)日志才能看到它。另请参阅this answer how to configure logging for JSF: JSF2 logs with tomcat

最常见的原因之一是文件不在您认为的位置。例如,路径确实无效。<ui:include>路径是相对于父文件的路径解析的。您应该努力在<ui:xxx>标签中使用绝对路径以避免在重构文件层次结构时出现维护问题,即从路径开始/,因此基本上使其相对于 web 内容根目录。例如

<ui:include src="/WEB-INF/includes/foo.xhtml" />

如果您绝对肯定路径是正确的,则另一个可能的原因是所需的包含文件是使用未知或错误的字符编码保存的,这会导致解析 XML 树时出现问题。您需要验证您的编辑器是否已正确配置为将所有文件保存为 UTF-8。

于 2013-03-19T11:58:31.627 回答