Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:03:46+00:00 2026-06-13T11:03:46+00:00

BACKGROUND I am learning Java and currently on JSF. I come from an ASP.NET

  • 0

BACKGROUND

I am learning Java and currently on JSF. I come from an ASP.NET MVC background and have only recently been exposed to the Java world, so be gentle 😉

PROBLEM

I can’t quite figure out how to get a model (which is of List<T>) into the JSF view. What I did when recently learning Struts was the following:

  1. Created a Servlet like this:

    @WebServlet(name = "ViewProductsServlet", urlPatterns = {"/ViewProductsServlet"})

    public class ViewProductsServlet extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            ProductRepository repository = new ProductRepository();
            List<Product> model = repository.getAll();
            request.setAttribute("model", model);
            request.getRequestDispatcher("/viewProducts.jsp").forward(request, response);
        }
    
  2. Linked to the view products page like this:

<html:link forward="viewProducts" styleClass="btn btn-primary">View Products</html:link>

  1. In the view, I got the model like this:

    <%
    List<Product> model = (List<Product>)request.getAttribute("model");
    %>

and then I just iterated over the items with JSTL tags.. not sure if that was the “correct” way to do it, but it worked..

Now for the JSF stuff, I am trying to take a similar approach but I noticed that the <% %> tags don’t work in xhtml pages. Fair enough, but then how do I pass a collection to a page so I can display the items in a table/grid?

EDIT

Thanks to Sanjeevi.V, I can see that I am supposed to use the h:dataTable tag. However, it is not displaying data. Here is my code:

MODEL/Manage Bean:

@ManagedBean
@SessionScoped
public class EmployeeCollection {
    private List<Employee> items;

    public List<Employee> getItems() {
        if (items == null) {
            EmployeeRepository repository = new EmployeeRepository();
            items = repository.getAll();
        }

        return items;
    }
}

View Markup:

<div class="container-fluid">
            <div class="row-fluid">
                <div class="span8 offset2">
                    <h:dataTable class="table table-striped" value="#{EmployeeCollection.items}" var="item">
                        <h:column>
                            <f:facet name="header">First Name</f:facet>
                            #{item.getFirstName()}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Last Name</f:facet>
                            #{item.getLastName()}
                        </h:column>
                        <h:column>
                            <f:facet name="header">Date of Birth</f:facet>
                            #{item.getDateOfBirth().toString()}
                        </h:column>
                    </h:dataTable>
                </div>
            </div>
        </div>

I tried to follow that tutorial, so I’m not sure where I am going wrong. Personally, I don’t understand how it could work.. how does the EmployeeCollection bean get wired up? Is it because it’s got @SessionScoped that there is only ever one per session and so will use that? Even so, at the time of running, it would be a null object. What am I missing here? How can I make this work? I do indeed have rows in the database, so don’t ask about that. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:03:47+00:00Added an answer on June 13, 2026 at 11:03 am

    Try the following code,

    Managed Bean:

    @ManagedBean(name = "bean")
    @SessionScoped
    public class Bean {
    private List<Integer> numList;
    
    public List<Integer> getNumList() {
        return numList;
    }
    
    public void setNumList(List<Integer> numList) {
        this.numList = numList;
    }
    
    public Bean() {
        numList=new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            numList.add((int)(Math.random()*100));
        }
    }
    
    }
    

    Markup:

    <h:dataTable value="#{bean.numList}" var="num">
      <h:column><h:outputText value="#{num}"/></h:column>
    </h:dataTable>
    

    source : http://www.mkyong.com/jsf2/jsf-2-datatable-example/

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently been learning vb.net, coming from a background in java and c. I'm
I have recently been thrown into learning ClearCase (I come from a background with
I come from a Java background and I am learning Objective C. I am
I'm learning Java from a webdev background. I've been using some HTML inside Swing
I have background of Java and Python and I'm learning R recently. Today I
Hi I have background in Java and currently learning C++. In Java it is
I come from a Java background and recently decided to try creating a C++
I just started learning Java very recently, coming from a C# background. When I
I'm learning ruby (have a java/groovy background) and it looks like when writing methods,
I am currently learning F# and functional programming in general (from a C# background)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.