我想在JSF中创建一个简单的图书管理。我正在使用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 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}
- &gt; BookController:at.em.controller.BookController@107dd383
错误:
/createBook.xhtml @ 13,82 value =“#{bookController.book.title}”:目标 无法访问,'null'返回null
答案 0 :(得分:4)
/createBook.xhtml @ 13,82 value =“#{bookController.book.title}”:目标无法访问,'null'返回null
#{bookController}
在范围内的任何位置都不可用。这通常是@Named
注释的责任。只有存在/WEB-INF/beans.xml
文件时,才会在webapp的启动时扫描此注释。因此请确保该文件存在(可以保留为空)。
或者,您也可以使用标准的JSF注释:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class BookController {
// ...
}
(请注意,我没有使用javax.faces.bean.SessionScoped
,因为这是普通控制器的错误范围;如果使用它,同一个bean实例将在同一个浏览器选项卡/窗口之间共享会话只会导致不直观的webapp行为)