搜索参数是名字和姓氏,而当我点击搜索时它正在获取值,因为我在控制台上打印但没有使用数据表显示。我为此目的编写的代码如下:
studentSearch.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Search the Student Here</title>
</h:head>
<h:body>
<h:form id="searchForm">
<center><b>Search the Student Details Here</b></center>
<h:panelGrid id="searchStudent" columns="4">
<h:outputLabel id="searchText" value="Enter the firstname of the Student:"></h:outputLabel>
<h:inputText id="searchfName" value="#{student.firstName}"></h:inputText>
<h:outputLabel id="searchlText" value="Enter the lastname of the Student:"></h:outputLabel>
<h:inputText id="searchfName" value="#{student.lastName}"></h:inputText>
<h:commandButton value="Search Student" action="#{student.fullInfoByname}"></h:commandButton>
<h:dataTable value="#{searchBean.lstSearch}" var="student">
<h:column>
<f:facet name="header">
Student School ID
</f:facet>
<h:outputText value="#{student.studentSchoolID}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
Student First Name
</f:facet>
#{student.firstName}
</h:column>
<h:column>
<f:facet name="header">
Student Last Name
</f:facet>
#{student.lastName}
</h:column>
<h:column>
<f:facet name="header">
Father's Name
</f:facet>
#{student.fatherName}
</h:column>
<h:column>
<f:facet name="header">
Mother's Name
</f:facet>
#{student.motherName}
</h:column>
<h:column>
<f:facet name="header">
Gender
</f:facet>
#{student.gender}
</h:column>
</h:dataTable>
</h:panelGrid>
</h:form>
</h:body>
</html>
用于此目的的方法 StudentDao.java方法
public List<Student> getStudentByName(String fname,String lname)
{
Transaction trans=null;
Session session=HiberUtil.getSessionFactory().openSession();
try{
trans=session.beginTransaction();
String queryString="from Student where firstName = :id or lastName = :idd";
Query query=session.createQuery(queryString);
query.setString("id", fname);
query.setString("idd", lname);
List<Student> list=query.list();
if(list.size()>0)
{
return list;
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
in Student.java class
public void fullInfoByname()
{
StudentDao dao=new StudentDao();
List<Student> list=new ArrayList<Student>();
List<Student> lc=dao.getStudentByName(firstName, lastName);
for(int i=0;i<lc.size();i++)
{
System.out.println(lc.get(0).firstName);
this.studentSchoolID=lc.get(0).studentSchoolID;
System.out.println(lc.get(0).studentSchoolID);
this.lastName=lc.get(0).lastName;
System.out.println(lc.get(0).lastName);
this.fatherName=lc.get(0).fatherName;
System.out.println(lc.get(0).fatherName);
this.motherName=lc.get(0).motherName;
System.out.println(lc.get(0).motherName);
this.gender=lc.get(0).gender;
System.out.println(lc.get(0).gender);
this.fee=lc.get(0).fee;
this.course=lc.get(0).course;
this.session=lc.get(0).session;
this.hobbies=lc.get(0).hobbies;
this.email=lc.get(0).email;
this.phoneNumber=lc.get(0).phoneNumber;
this.addressOne=lc.get(0).addressOne;
this.addressTwo=lc.get(0).addressTwo;
this.city=lc.get(0).city;
this.state=lc.get(0).state;
this.zip=lc.get(0).zip;
this.country=lc.get(0).country;
this.status=lc.get(0).status;
}
list.addAll(lc);
System.out.println("list Size="+list.size());
if(list.size()>0)
{
SearchStudent sc=new SearchStudent();
sc.setLstSearch(list);
}
}
现在在最后我将列表sc.setLstSearch放入xhtml页面后,它没有显示搜索值,而如果我得到变量lstSearch的大小,那么它给出了正确的大小但没有显示它。 searchBean中创建的列表如下所示。
package com.school.entity;
import java.util.Iterator;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="searchBean")
@SessionScoped
public class SearchStudent {
private List<Student> lstSearch;
public List<Student> getLstSearch() {
return lstSearch;
}
public void setLstSearch(List<Student> lstSearch) {
this.lstSearch = lstSearch;
//System.out.println(lstSearch);
//for checking how many values coming from the list
/*Iterator<Student> itr=lstSearch.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}*/
}
}
答案 0 :(得分:0)
问题出在Student类中,您不应该使用new运算符创建SearchStudent类。尝试使用managedProperty注入它并进行设置。如果您不知道如何使用它,请按照本教程http://www.mkyong.com/jsf2/injecting-managed-beans-in-jsf-2-0/
进行操作<强>更新强>
在您发表评论后,我注意到的是,Student.java是一个实体类。将业务逻辑放在实体类中并且在实体类中不允许注入也不好。因此,请按以下步骤更新您的代码
public void fullInfoByname()
移至托管bean,即SearchStudent
类。firstName
class lastName
和SearchStudent
SearchStudent
class。fullInfoByname
。this.setLstSearch(list);
方法fullInfoByname
醇>