Search code examples
databasehibernateselectstruts2auto-populate

How to populate select from database in Struts2+Hibernate?


I'm using Struts2+Hibernate. I have a form in a JSP page, in which there is a select that I need to populate it from Database. I have implemented the DAO class BookDAO ( selectBooks(), updateBook(Book book)). I have created the Action class in which I declared an ArrayList of Book, and an object of class BookDAO. It seems that I need to define a function in the Action class which call selectBooks and populate my ArrayList, But this action should be called automatically on loading my JSP page. Is Ajax necessary in my case? Thank you.


Solution

  • No, AJAX is not necessary. In the code of your action method, initialize the list:

    public String execute() {
        this.books = bookDAO.selectBooks(); 
        return SUCCESS;
    }
    

    The JSP page will then have access to the list of books.