JSF和Hibernate从数据库中查找特定记录并显示所有数据的问题[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我编写了一些代码,可以使用JSF,Hibernate和MySQL在数据库中执行CRUD功能。唯一的问题是,当我实现findgetAllRecords时,应用程序不起作用。我的一些代码如下:

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:集合操作的非法语法

java hibernate jsf datatable propertynotfoundexception
1个回答
0
投票

你将在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>
© www.soinside.com 2019 - 2024. All rights reserved.