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 リンク ( How to include another XHTML in XHTML using JSF 2.0 Facelets? ) をたどったとき、問題は 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 サーブレットの前に呼び出されるため、たまたまコール スタックにあるだけです。

この特定の例外はFaceletContext#includeFacelet()IOException. IOException残念ながら、これは根本的な原因としてラップされておらず、ログオンFINEレベルのみです。ログを表示するには、ログをオンFINE(またはALL) にする必要があります。この回答も参照してください JSFのロギングを構成する方法: Tomcatを使用したJSF2ログ

最も一般的な原因の 1 つは、ファイルがあるはずの場所にないことです。たとえば、パスは実際には無効です。<ui:include>パスは、親ファイルのパスに対して相対的に解決されます。<ui:xxx>ファイル階層を再構築するときのメンテナンスの問題を回避するために、タグで絶対パスを使用するように努める必要があります。つまり、パスを で開始し/、基本的に Web コンテンツ ルートに相対的にします。例えば

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

パスが正しいと確信している場合、別の原因として考えられるのは、目的のインクルード ファイルが未知または間違った文字エンコーディングを使用して保存されていることです。これにより、XML ツリーの解析中に問題が発生します。すべてのファイルを UTF-8 として保存するようにエディターが適切に構成されているかどうかを確認する必要があります。

于 2013-03-19T11:58:31.627 に答える