0

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.

1
  • Post your jsp page Commented Apr 13, 2019 at 12:27

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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());
%>

4 Comments

I was thinking of accessing the list through the Model bookModel from the Controller... How can I do that?
Please post the code of Model and BookDao classes also and explain your intent little more clearly.
I think you need to simplify stuffs a little bit. You need to create your Model and Controller properly. Read the following article for understanding Model View Controller concept: baeldung.com/mvc-servlet-jsp
Your controller should be a Servlet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.