<ui:include>抛出javax.faces.view.facelets.TagAttributeException:无效路径

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();
    }
}

我该如何修复这个问题?
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)日志才能看到它。另请参阅此答案以了解如何为 JSF 配置日志记录:JSF2 logs with tomcat

最常见的原因之一是文件不在你认为它在的位置。例如,路径实际上是无效的。<ui:include>路径相对于父文件的路径进行解析。您应该努力在<ui:xxx>标记中使用绝对路径,以避免在重新构造文件层次结构时出现维护问题,即以/开头并使其基本上相对于webcontent根目录。例如:

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

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

嗨,BalusC。所有路径已经正确定义,但是通过您精彩的解释,我对问题(UTF-8问题)有了一些提示。我所做的就是从模板文件中删除一些特殊的(葡萄牙语)字符,现在它可以工作了!非常感谢! - jMarcel
不用客气。JSF2/Facelets 默认使用 UTF-8 编码。因此强烈建议您将编辑器配置为以 UTF-8 格式保存文件。这样,在 Facelets 文件中使用“特殊”字符时就不会像那样崩溃了。 - BalusC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接