在 JSF 页面中显示实体列表
Display a List of entities in JSF page
我正在尝试使用 JSF 显示 List<Student>
。
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 页面:
<h:form>
<h:outputText value="#{student.retrieveStudent()}"/>
</h:form>
输出:
[Alpha,Beta,Science,Electronics]
我想将其打印在 table 中,列名称为名称(Alpha,Beta),第 2 列是他们的部门。我不确定如何将它们分开并打印...谁能帮我...
实际上,这种情况下的约定是在 bean 中而不是在 Student.java
class 中创建属性。此外,您还必须有一个名为 getNameOfAttribute()
的方法才能在视图中使用(在本例中为 getStudents()
)。
您在输出中看到的内容:[Alpha,Beta,Science,Electronics]
是 retrieveStudent()
方法检索到的 stud
列表的 toString()
表示。
你要做的就是把这个列表放在 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>
注意: 学生必须实施 public 方法 getName()
和 getDeparment()
.
我正在尝试使用 JSF 显示 List<Student>
。
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 页面:
<h:form>
<h:outputText value="#{student.retrieveStudent()}"/>
</h:form>
输出:
[Alpha,Beta,Science,Electronics]
我想将其打印在 table 中,列名称为名称(Alpha,Beta),第 2 列是他们的部门。我不确定如何将它们分开并打印...谁能帮我...
实际上,这种情况下的约定是在 bean 中而不是在 Student.java
class 中创建属性。此外,您还必须有一个名为 getNameOfAttribute()
的方法才能在视图中使用(在本例中为 getStudents()
)。
您在输出中看到的内容:[Alpha,Beta,Science,Electronics]
是 retrieveStudent()
方法检索到的 stud
列表的 toString()
表示。
你要做的就是把这个列表放在 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>
注意: 学生必须实施 public 方法 getName()
和 getDeparment()
.