简体   繁体   中英

How to display ArrayList items from java in jsp?

I have generated an Arraylist, where I get items from database row by row. I want to display the values in jsp, but I don't know how to bind jsp to java.

In java class, listBook is an Arraylist of type BookBean.

BookDao class:

public  ArrayList<BookBean> listBooks = new ArrayList<>();

....

System.out.println(listBooks.get(0).getId()); ->display id of first row
System.out.println(listBooks.get(0).getTitle()); ->display title of first row
System.out.println(listBooks.get(0).getAuthor());

In my Controller class, i have:

    public String showBooks(Model bookModel){
        bookModel.addAttribute("bookAttribute", new BookDao());
        return "book-list";
    }

I want to print the results of listBook in jsp by using the Model from the Controller. How can I do that?

BookDao:
public  ArrayList<BookBean> listBooks = new ArrayList<>();
 public void generateBookList() {
        try {
            Connection connection = ConnectToDatabase.createConnection();
            if (connection != null) {
                PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from book ");
                ResultSet resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    BookBean bookBean = new BookBean(resultSet.getInt("id_book"), resultSet.getString("title"), resultSet.getString("author"), resultSet.getString("publishDate"), resultSet.getInt("quantity"), resultSet.getString("bookPrice"));
                    listBooks.add(bookBean);
                } }
        } catch (Exception e) {
            e.printStackTrace();
        }}

BookController to open the jsp page "book-list.jsp":

@Controller
public class BookController {
    @RequestMapping("/showBooks")
    public String showBooks(Model bookModel){
        bookModel.addAttribute("bookAttribute", new BookDao());
        return "book/book-list";
    }
}

I want to access the "listBooks" in jsp trough the Model created in the controller. I was thinking of jstl, but I cannot manage to write the code accordingly.

You can use jstl core tag <c:forEach> . If you need loop over your list you can do something like this:

In your BookController pass your list to the model:

 model.addAttribute("bookList", yourList);

In JSP:

...
<c:forEach items="${bookList}" var="book"> 
${book.id}  <%-- BookBean fields that you want print out--%>
${book.title}
<%-- another fields --%>
</c:forEach>
...

see official oracle documentation for more detail

On a JSP page simply do the following:

<%
   out.println(listBooks.get(0).getId()); ->display id of first row
   out.println(listBooks.get(0).getTitle()); ->display title of first row
   out.println(listBooks.get(0).getAuthor());
%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM