JSF和Hibernate从数据库中查找特定记录并显示所有数据的问题[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSF和Hibernate从数据库中查找特定记录并显示所有数据的问题[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我编写了一些代码,可以使用JSF,Hibernate和mysql在数据库中执行CRUD功能。唯一的问题是,当我实现find
和getAllRecords
时,应用程序不起作用。我的一些代码如下:
BookController.java类
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类
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:类'java.lang.String'没有属性'book_isbn'。
我得到的findBook的错误是
javax.el.PropertyNotWritableException:集合操作的非法语法
你将在String
返回一个getAllBook()
,所以你应该返回一个ArrayList<Book>
(getAllBook()
类的BookController.java
)
public ArrayList<Book> getAllBook() throws Exception{
BookManager bm = new BookManager();
return bm.getAllBooks();
}
而对于查找页面,同样的事情,在BookController.java
中你的方法不应该返回一个String
,你必须返回一个Book对象:
public void findBook() throws Exception{
BookManager bm = new BookManager();
Book book = bm.findBook(isbn);
this.title = book.getTitle();
}
.
将update
属性添加到commandButton。
<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从数据库中查找特定记录并显示所有数据的问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何在spring boot和hibernate中计算特定id的记录
在 JSF2.0、Hibernate、MySQL 中启用 UTF-8 字符集