JSF 和 Hibernate 从数据库中查找特定记录并显示所有数据的问题
JSF and Hibernate Issue in finding a specific record from database and displaying all data
我编写了一些代码,可以使用 JSF、Hibernate 和 MySQL 在数据库中执行 CRUD 功能。唯一的问题是,当我实施 find
和 getAllRecords
时,应用程序无法运行。我的一些代码如下:
BookController.java Class
public class BookController {
private int isbn;
private String title;
public int getIsbn() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String findBook() throws Exception{
BookManager bm = new BookManager();
bm.findBook(isbn);
return "find";
}
public String getAllBook() throws Exception{
BookManager bm = new BookManager();
bm.getAllBooks();
return "all";
}
}
BookManager.java Class
public class BookManager {
private SessionFactory sessionFactory;
public BookManager() throws Exception{
//A sessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure().buildSessionFactory(); // configures settings from hibernate.cfg.xml
}
public int addBook(int isbn, String title){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = new Book();
b.setIsbn(isbn);
b.setTitle(title);
int BookID = (Integer)session.save(b);
tx.commit();
session.close();
return BookID;
}
public void updateBook(int isbn, String title){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = new Book();
b.setIsbn(isbn);
b.setTitle(title);
session.update(b);
tx.commit();
session.close();
}
public Book findBook(int isbn){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = (Book)session.get(Book.class, isbn);
tx.commit();
session.close();
return b;
}
public void deleteBook(int isbn){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = (Book)session.get(Book.class, isbn);
session.delete(b);
tx.commit();
session.close();
}
@SuppressWarnings("unchecked")
public ArrayList<Book> getAllBooks(){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
ArrayList<Book> allBooks = (ArrayList<Book>)session.createQuery("from Book").list();
tx.commit();
session.close();
return allBooks;
}
}
Find.jsp
<h:form>
ISBN: <h:inputText value = "#{bookController.isbn} "/> <br>
<h:commandButton action="#{bookController.findBook}" value="find" /> <br>
Book Found is <h:outputLabel value="#{bookController.title }" />
</h:form>
getAllBooks.jsp
<h:form>
<h:dataTable value = "#{bookController.allBook }" var = "all1">
<f:facet name = "header">
<h:outputText value="Existing Books" />
</f:facet>
<h:column>
<f:facet name = "header">
<f:verbatim>ISBN </f:verbatim>
</f:facet>
<h:outputText value="#{all1.book_isbn }" /> <!-- book_isbn column in table db -->
</h:column>
<h:column>
<f:facet name = "header">
<f:verbatim>Title</f:verbatim>
</f:facet>
<h:outputText value="#{all1.book_title }" /> <!-- book_title column in table db -->
</h:column>
</h:dataTable>
</h:form>
我得到的 getAllBooks 错误是
javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'book_isbn'.
我得到的 findBook 错误是
javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
您正在 return getAllBook()
中 String
,因此您应该 return ArrayList<Book>
(getAllBook()
BookController.java
class )
public ArrayList<Book> getAllBook() throws Exception{
BookManager bm = new BookManager();
return bm.getAllBooks();
}
对于查找页面,同样的事情,在 BookController.java
中你的方法不应该 return 一个 String
,你必须 return 一个 Book 对象:
public void findBook() throws Exception{
BookManager bm = new BookManager();
Book book = bm.findBook(isbn);
this.title = book.getTitle();
}
.
将 update
属性添加到您的 comandButton 。
<h:form>
ISBN: <h:inputText value = "#{bookController.isbn} "/> <br>
<h:commandButton action="#{bookController.findBook}" value="find" update="@form" /> <br>
Book Found is <h:outputLabel value="#{bookController.title }" />
</h:form>
我编写了一些代码,可以使用 JSF、Hibernate 和 MySQL 在数据库中执行 CRUD 功能。唯一的问题是,当我实施 find
和 getAllRecords
时,应用程序无法运行。我的一些代码如下:
BookController.java Class
public class BookController {
private int isbn;
private String title;
public int getIsbn() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String findBook() throws Exception{
BookManager bm = new BookManager();
bm.findBook(isbn);
return "find";
}
public String getAllBook() throws Exception{
BookManager bm = new BookManager();
bm.getAllBooks();
return "all";
}
}
BookManager.java Class
public class BookManager {
private SessionFactory sessionFactory;
public BookManager() throws Exception{
//A sessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure().buildSessionFactory(); // configures settings from hibernate.cfg.xml
}
public int addBook(int isbn, String title){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = new Book();
b.setIsbn(isbn);
b.setTitle(title);
int BookID = (Integer)session.save(b);
tx.commit();
session.close();
return BookID;
}
public void updateBook(int isbn, String title){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = new Book();
b.setIsbn(isbn);
b.setTitle(title);
session.update(b);
tx.commit();
session.close();
}
public Book findBook(int isbn){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = (Book)session.get(Book.class, isbn);
tx.commit();
session.close();
return b;
}
public void deleteBook(int isbn){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Book b = (Book)session.get(Book.class, isbn);
session.delete(b);
tx.commit();
session.close();
}
@SuppressWarnings("unchecked")
public ArrayList<Book> getAllBooks(){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
ArrayList<Book> allBooks = (ArrayList<Book>)session.createQuery("from Book").list();
tx.commit();
session.close();
return allBooks;
}
}
Find.jsp
<h:form>
ISBN: <h:inputText value = "#{bookController.isbn} "/> <br>
<h:commandButton action="#{bookController.findBook}" value="find" /> <br>
Book Found is <h:outputLabel value="#{bookController.title }" />
</h:form>
getAllBooks.jsp
<h:form>
<h:dataTable value = "#{bookController.allBook }" var = "all1">
<f:facet name = "header">
<h:outputText value="Existing Books" />
</f:facet>
<h:column>
<f:facet name = "header">
<f:verbatim>ISBN </f:verbatim>
</f:facet>
<h:outputText value="#{all1.book_isbn }" /> <!-- book_isbn column in table db -->
</h:column>
<h:column>
<f:facet name = "header">
<f:verbatim>Title</f:verbatim>
</f:facet>
<h:outputText value="#{all1.book_title }" /> <!-- book_title column in table db -->
</h:column>
</h:dataTable>
</h:form>
我得到的 getAllBooks 错误是
javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'book_isbn'.
我得到的 findBook 错误是
javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
您正在 return getAllBook()
中 String
,因此您应该 return ArrayList<Book>
(getAllBook()
BookController.java
class )
public ArrayList<Book> getAllBook() throws Exception{
BookManager bm = new BookManager();
return bm.getAllBooks();
}
对于查找页面,同样的事情,在 BookController.java
中你的方法不应该 return 一个 String
,你必须 return 一个 Book 对象:
public void findBook() throws Exception{
BookManager bm = new BookManager();
Book book = bm.findBook(isbn);
this.title = book.getTitle();
}
.
将 update
属性添加到您的 comandButton 。
<h:form>
ISBN: <h:inputText value = "#{bookController.isbn} "/> <br>
<h:commandButton action="#{bookController.findBook}" value="find" update="@form" /> <br>
Book Found is <h:outputLabel value="#{bookController.title }" />
</h:form>