How do I display a list of items from a Bean onto a JSF webpage?

前端 未结 2 1587
失恋的感觉
失恋的感觉 2020-12-05 18:41

I\'m new to JSF and in the process of learning im building an online book store application.

I have 1 class and 1 bean: Book.java and BookCatelog

2条回答
  •  既然无缘
    2020-12-05 19:29

    JSF2 offers two iterating components out the box: and . The former renders nothing to the response (so you have 100% control over the final HTML output), while the latter renders a HTML

    to the response and requires a to represent a column of
    s. Both components can take among others a List as value.

    So, you can just have your managed bean like follows:

    @ManagedBean
    @RequestScoped
    public class BookCatalog implements Serializable {
    
        private List books;
    
        @PostConstruct
        public void init() {
            books = new ArrayList();
            books.add(new Book(1, "Theory of Money and Credit", "Ludwig von Mises"));
            books.add(new Book(2, "Man, Economy and State", "Murry Rothbard"));
            books.add(new Book(3, "Real Time Relationships", "Stefan Molyneux"));
        }
    
        public List getBooks() {
            return books;
        }
    
    }
    

    And you can use to generate for example an

    • :

      (note that the var attribute basically exposes the currently iterated item by exactly the given name in the EL scope within the component)

      And here's how to use a instead:

      
          
              
                  
              
          
      
      

      As to the JSTL , that's also quite possible, but you should keep in mind that JSTL tags have a different lifecycle than JSF components. Long story short: JSTL in JSF2 Facelets... makes sense?

      See also:

      • How to choose the right bean scope?
      • How and when should I load the model from database for h:dataTable
      • Creating master-detail pages for entities, how to link them and which bean scope to choose

    提交回复
    热议问题