1

I am trying to display a List<Student> using JSF.

public List<Student> retrieveStudent()
{
Sdao sdao=new Sdao();
List<Student> stud=new ArrayList();
List sname1=new ArrayList();
List sdept1=new ArrayList();
for(Student show1:sdao.retrieveStudent())
{

    sname1.add(show1.getSname());
    sdept1.add(show1.getSdept());

}
stud.addAll(sname1);
stud.addAll(sdept1);


return stud;

}

JSF Page:

    <h:form>

        <h:outputText value="#{student.retrieveStudent()}"/>
     </h:form> 

Output:

[Alpha,Beta,Science,Electronics]

I want to print it in a table with column name Names(Alpha, Beta) and column 2 their departments..i am not sure how to separate them and print...can anyone help me...

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JavaLearner
  • 272
  • 1
  • 11

1 Answers1

1

Actually, convention for this cases is to create an attribute in the bean not in the Student.java class. Also you must have a method called getNameOfAttribute() to use in the view (in this case getStudents()).


What you see in the output: [Alpha,Beta,Science,Electronics] is the toString() representation of the stud List retrieved by retrieveStudent() method.

What you have to do is put this list in DataTable:

<h:dataTable value="#{userData.retrieveStudent()}" var="student" >
   <h:column>                   
      <f:facet name="header">Name</f:facet>                 
      #{student.name}
   </h:column>
   <h:column>
      <f:facet name="header">Department</f:facet>
      #{student.department}
   </h:column>
</h:dataTable>

NOTE: student must implement public methods getName() and getDeparment().

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109