0

I want to create a simple Book Management in JSF. I am using Glashfish Server 3.1

Book Controller:

@Named(value = "bookController")
@SessionScoped
public class BookController implements Serializable {

        @EJB
        BookFacadeLocal bookFacade;
        Book book = new Book();
        private List<Book> booklist = new LinkedList<Book> ();
    /** Creates a new instance of BookController */
    public BookController() {


    }

    public Book getBook() {
        if (book == null)
    book = new Book();
    return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public List<Book> getBooklist() {
        return booklist;
    }

    public void setBooklist(List<Book> booklist) {
        this.booklist = booklist;
    }
    public String createNewBook()
        {
            setBook(new Book());
            return "createBook";
        }
    public String saveNewBook()
    {
            bookFacade.create(book);
            booklist=bookFacade.findAll();
            return "listBooks";
    }
        public String createBookList()
    {
            booklist=bookFacade.findAll();
            return "listBooks";
    }
}

CreateBook View:

<?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://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Create Book
            <h:form id="createBook">
                <h:panelGrid id="grid" columns="2" >
                    <h:outputLabel value="Title" for="title"></h:outputLabel>
                    <h:inputText id="title" value="#{bookController.book.title}"></h:inputText>
                    <h:outputLabel value="Price" for="price"></h:outputLabel>
                    <h:inputText id="price" value="#{bookController.book.price}"></h:inputText>
                    <h:outputLabel value="Description" for="description"></h:outputLabel>
                    <h:inputTextarea id="description" value="#{bookController.book.description}"></h:inputTextarea>
                    <h:outputLabel value="ISBN" for="isbn"></h:outputLabel>
                    <h:inputText id="isbn" value="#{bookController.book.isbn}"></h:inputText>
                    <h:outputLabel value="Pages" for="pages"></h:outputLabel>
                    <h:inputText id="pages" value="#{bookController.book.nbOfPage}"></h:inputText>
                    <h:outputLabel value="Illustration" for="illustrations"></h:outputLabel>
                    <h:selectBooleanCheckbox id="illustrations" value="#{bookController.book.illustrations}"> </h:selectBooleanCheckbox>
                    <h:panelGroup> </h:panelGroup>
                    <h:commandButton id="submit" value ="Save" action="#{bookController.saveNewBook}"> </h:commandButton>
                </h:panelGrid>
            </h:form>
    </h:body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

BookController: #{bookController}

--> BookController: at.em.controller.BookController@107dd383

Error:

/createBook.xhtml @13,82 value="#{bookController.book.title}": Target Unreachable, 'null' returned null

test123123
  • 901
  • 3
  • 16
  • 33

1 Answers1

4

/createBook.xhtml @13,82 value="#{bookController.book.title}": Target Unreachable, 'null' returned null

The #{bookController} is not available anywhere in the scope. This is normally the responsibility of @Named annotation. This annotation will only be scanned on webapp's startup when there's a /WEB-INF/beans.xml file present. So make sure that that file is present (it can be kept empty).

Alternatively, you can also just use the standard JSF annotations:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class BookController {

    // ...

}

(note that I didn't use javax.faces.bean.SessionScoped because that's the wrong scope for a normal controller; if it was used, the same bean instance would be shared among multiple browser tabs/windows in the same session which would only lead to unintuitive webapp behaviour)

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • hy! /WEB-INF/beans.xml exists with some xml declarations in it... i changed the annotations in my controller but the same error occured. – test123123 Jan 04 '12 at 14:40
  • What did you declare in `beans.xml`? It sounds like that you have duplicated the bean registration which would override the annotation altogether. – BalusC Jan 04 '12 at 14:43
  • So neither CDI annotations nor JSF annotations work? What servlet container are you using? (should be servlet 3.0 compatible, e.g. Glassfish 3, Tomcat 7, etc). Which servlet version is your `web.xml` declared to? (should be 3.0). If all is right, then perhaps EL resolving is simply broken. Which configuration do you all have in `web.xml`? What do you see in the browser if you just print the bean instance as in `

    BookController: #{bookController}

    `?
    – BalusC Jan 04 '12 at 14:58
  • Which servlet container are you using? – BalusC Jan 04 '12 at 16:06
  • I am using Glashfish Server 3.1 – test123123 Jan 07 '12 at 11:12