gpt4 book ai didi

java - 目标无法访问, 'null' 在 JSF 中返回 null

转载 作者:行者123 更新时间:2023-12-01 19:09:41 25 4
gpt4 key购买 nike

我想在 JSF 中创建一个简单的图书管理。我使用的是 Glashfish Server 3.1

图书 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";
}
}

创建图书 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

错误:

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

最佳答案

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

#{bookController} 在范围内的任何地方都不可用。这通常是 @Named 注释的职责。仅当存在 /WEB-INF/beans.xml 文件时,才会在 web 应用程序启动时扫描此注释。因此,请确保该文件存在(可以保留为空)。

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

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

@ManagedBean
@ViewScoped
public class BookController {

// ...

}

(请注意,我没有使用 javax.faces.bean.SessionScoped 因为这对于普通 Controller 来说是错误的作用域;如果使用它,则会共享同一个 bean 实例在同一 session 中的多个浏览器选项卡/窗口之间,这只会导致不直观的 Web 应用程序行为)

关于java - 目标无法访问, 'null' 在 JSF 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com