我想在panelgrid中调用动态值而不是datatable。在datatable中我得到动态值,但我想在panelgrid中调用动态值。如果有人知道如何在panelgrid中访问动态值,那么给我回答。
java class:
public List<Student> getStuDetails() {
Transaction trns = null;
students = new ArrayList<Student>();
Session session = getSession();
try {
trns = session.beginTransaction();
String sql = "select name,age,profession from student where sid = " + sid;
SQLQuery query = session.createSQLQuery(sql).addScalar("name", new StringType()).addScalar("age", new IntegerType()).addScalar("profession", new StringType());
List<Object[]> rows = query.list();
for (Object[] row : rows) {
Student student = new Student();
student.setName(row[0].toString());
student.setAge(Integer.parseInt(row[1].toString()));
student.setProfession(row[2].toString());
students.add(student);
}
trns.commit();
} catch (HibernateException e) {
e.printStackTrace();
trns.rollback();
} finally {
session.flush();
session.close();
}
return students;
}
jsf代码:
<p:panelGrid id="pgrid" columns="2">
<p:outputLabel value="Name:"/>
<p:outputLabel value="Austin"/>
<p:outputLabel value="Age:"/>
<p:outputLabel value="26"/>
<p:outputLabel value="Profession"/>
<p:outputLabel value="job"/>
</p:panelGrid>
答案 0 :(得分:0)
我修改了你的代码并为我工作。我创建了三个变量sname,sage和sprofession,还需要创建getter / setter,这里我采用的是托管bean名称&#34; student&#34; 。
更新了Java类:
private String sname;
private Integer sage;
private Integer sprofession;
public List<Student> getStuDetails() {
Transaction trns = null;
students = new ArrayList<Student>();
Session session = getSession();
try {
trns = session.beginTransaction();
String sql = "select name,age,profession from student where sid = " + sid;
SQLQuery query = session.createSQLQuery(sql).addScalar("name", new StringType()).addScalar("age", new IntegerType()).addScalar("profession", new StringType());
List<Object[]> rows = query.list();
for (Object[] row : rows) {
Student student = new Student();
student.setName(row[0].toString());
student.setAge(Integer.parseInt(row[1].toString()));
student.setProfession(row[2].toString());
students.add(student);
}
sname = students.get(0).setName();
sage = students.get(0).setAge();
sprofession = students.get(0).setProfession();
trns.commit();
} catch (HibernateException e) {
e.printStackTrace();
trns.rollback();
} finally {
session.flush();
session.close();
}
return students;
}
更新了panelgrid:
<p:panelGrid id="pgrid" columns="2">
<p:outputLabel value="Name:"/>
<p:outputLabel value="#{student.sname}"/>
<p:outputLabel value="Age:"/>
<p:outputLabel value="#{student.sage}"/>
<p:outputLabel value="Profession"/>
<p:outputLabel value="#{student.sprofession}"/>
</p:panelGrid>
注意:但是如果您将使用panelgrid而不是datatable,那么将来您无法导出panelgrid数据,因为不支持panelexporter panelgrid或面板。