Target Unreachable,'null'在JSF中返回null [英] Target Unreachable, 'null' returned null in JSF

查看:68
本文介绍了Target Unreachable,'null'在JSF中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JSF中创建一个简单的Book Management.我正在使用Glashfish Server 3.1

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

图书控制器:

@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视图:

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: #{bookController}

错误:

/createBook.xhtml @ 13,82 value =#{bookController.book.title}":目标 无法访问,"null"返回null

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

推荐答案

/createBook.xhtml @ 13,82 value =#{bookController.book.title}":无法到达目标,"null"返回null

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

#{bookController}在示波器的任何位置都不可用.这通常是@Named注释的责任.仅当存在/WEB-INF/beans.xml文件时,才会在webapp启动时扫描此批注.因此,请确保该文件存在(可以将其保留为空).

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).

或者,您也可以只使用标准的JSF批注:

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 {

    // ...

}

(请注意,我没有使用javax.faces.bean.SessionScoped,因为这是普通控制器的错误范围;如果使用它,则同一bean实例将在同一会话中的多个浏览器选项卡/窗口之间共享只会导致不直观的webapp行为)

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

这篇关于Target Unreachable,'null'在JSF中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆