0

I'm using Jakarta EE 9, Glassfish 6.0, and JSF 3.0 and getting "Target Unreachable, identifier '' resolved to null..." I know questions like this have been asked before but I tried all proposed solutions. Also, I could find very few discussions about version 3.0 of JSF. I would appreciate it if someone could help me spot what wrong is.

BookController.java

import entities.Book;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Inject;
import jakarta.inject.Named;

@Named
@RequestScoped
public class BookController{

    private String test = "dada";

    @Inject
    private BookEJB bookEJB;
    private Book book = new Book();

    public String doCreateBook(){

        bookEJB.createBook(book);

        FacesContext.getCurrentInstance().addMessage(
                null, new FacesMessage("Book created"));

        return "newBook.xhtml";
    }

    public void doFindBookById(){
        book = bookEJB.findBookById(book.getId());
    }

    public Book getBook() {
        return book;
    }
}

BookEJB.java

import entities.Book;
import jakarta.annotation.sql.DataSourceDefinition;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

import java.util.ArrayList;
import java.util.List;

@Named
@jakarta.ejb.Stateless(name = "BookEJB")
public class BookEJB {

    @PersistenceContext
    private EntityManager em;

    public BookEJB() {

    }

    public Book createBook(Book book){

        em.persist(book);
        return book;
    }

    public List<Book> findAllBooks() {
        return em.createNamedQuery("findAllBooks", Book.class).getResultList();
    }

    public Book findBookById(Long id){
        return em.find(Book.class, id);
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="3.0" xmlns="https://jakarta.ee/xml/ns/jakartaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
    https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_3_0.xsd">

</faces-config>

beans.xml (in both WEB-INF and META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd"
       version="3.0"
       bean-discovery-mode="all">
</beans>

layout.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
   <title><ui:insert name="title">Default title</ui:insert></title>
   <link rel="icon" href="https://i.loli.net/2021/06/12/im236Uy7FLhxCJ1.png"/>
</h:head>
<h:body>
   <h:link value="Create a book" outcome="newBook.xhtml"/>

   <h1><ui:insert name="title">Default title</ui:insert></h1>
   <hr/>

<!--   <h:message for="errors"/>-->

   <ui:insert name="content">Default content</ui:insert>
   <hr/>
   <h:outputText value="APress"></h:outputText>
</h:body>
</html>

newBook.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <ui:composition template="layout.xhtml">
    <ui:define name="title">Create a new book</ui:define>

    <ui:define name="content">

        <h:outputText value="#{bookController.test}"/>

        <h:form id="bookForm">
            <h:outputLabel value="Title: "/>
            <h:inputText value="#{bookController.book.title}"/>

            <h:outputLabel value="Price : "/>
            <h:inputText value="#{bookController.book.price}"/>

            <h:commandButton value="Create a book" action="#{bookController.doCreateBook}">
                <f:ajax execute="@form" render=":booklist :errors"/>
            </h:commandButton>
        </h:form>

        <hr/>
        <h1>List of Books</h1>

        <h:dataTable id="booklist" value="#{bookEJB.findAllBooks()}" var="bk">

            <h:column>
                <f:facet name="header">
                    <h:outputText value="Title"/>
                </f:facet>
                <h:link outcome="viewBook.xhtml?id=#{bk}" value="#{bk.title}"/>
            </h:column>

        </h:dataTable>

    </ui:define>
</ui:composition>
</html>
Richard Wilson
  • 297
  • 4
  • 17

1 Answers1

-1

It is because of Application Server that cannot fully support to JSF-3.0. I've got same error when I tested jakartaee-tutorial-examples/web/jsf/guessnumber-jsf project. I've tried guessnumber-jsf.war file on WildFly 24.0.0.Final and wildfly-23.0.2.Final Application servers. I got same error on both servers. But when I tested on Wildfly-preview-23.0.2.Final which supports JakartaEE 9.1 and JDK 11, it is working well.

Zaw Htoon
  • 59
  • 1
  • 5
  • This answer is incorrect. Re-read the question. It starts with *"I'm using Jakarta EE 9, Glassfish 6.0"*. – BalusC Jun 29 '21 at 13:11