我正在用JSF做一个项目,为了测试我的项目我做了一个接口,我用一个托管bean在一个简单的页面xhtml中显示数据库中定义的实体的内容,我已经放了EJB要使用该服务,但页面测试中没有任何内容,请帮助我。
实体:
@Entity
@NamedQuery(name="Patient.findAll", query="SELECT p FROM Patient p")
public class Patient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="id_patient")
private int idPatient;
private String adresse;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="date_naissance")
private Date dateNaissance;
private String email;
@Column(name="groupe_sanguin")
private String groupeSanguin;
private String login;
private String nom;
@Column(name="num_secu_sociale")
private String numSecuSociale;
private String password;
private String prenom;
@Column(name="regime_assurance")
private String regimeAssurance;
private String tel;
public Patient() {
}
public int getIdPatient() {
return this.idPatient;
}
public void setIdPatient(int idPatient) {
this.idPatient = idPatient;
}
public String getAdresse() {
return this.adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public Date getDateNaissance() {
return this.dateNaissance;
}
public void setDateNaissance(Date dateNaissance) {
this.dateNaissance = dateNaissance;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGroupeSanguin() {
return this.groupeSanguin;
}
public void setGroupeSanguin(String groupeSanguin) {
this.groupeSanguin = groupeSanguin;
}
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getNumSecuSociale() {
return this.numSecuSociale;
}
public void setNumSecuSociale(String numSecuSociale) {
this.numSecuSociale = numSecuSociale;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPrenom() {
return this.prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getRegimeAssurance() {
return this.regimeAssurance;
}
public void setRegimeAssurance(String regimeAssurance) {
this.regimeAssurance = regimeAssurance;
}
public String getTel() {
return this.tel;
}
@Override
public String toString() {
return "Patient [idPatient=" + idPatient + ", adresse=" + adresse + ", dateNaissance=" + dateNaissance
+ ", email=" + email + ", groupeSanguin=" + groupeSanguin + ", login=" + login + ", nom=" + nom
+ ", numSecuSociale=" + numSecuSociale + ", password=" + password + ", prenom=" + prenom
+ ", regimeAssurance=" + regimeAssurance + ", tel=" + tel + "]";
}
public void setTel(String tel) {
this.tel = tel;
}
}
ConsulterDossierInter(界面):
@Remote
public interface ConsulterDossierInter {
public List<Patient> getAllPatients();
public Patient findPatient(String nom);
}
ConsulterDossier:
@Stateless
public class ConsulterDossier implements ConsulterDossierInter {
@PersistenceContext
private EntityManager em;
public ConsulterDossier() {
// TODO Auto-generated constructor stub
}
@SuppressWarnings("unchecked")
@Override
public List<Patient> getAllPatients() {
// TODO Auto-generated method stub
Query query=em.createQuery("SELECT p FROM Patient p");
return query.getResultList();
}
@Override
public Patient findPatient(String nom) {
Query q=em.createQuery("SELECT p FROM Patient p WHERE p.nom=:nom");
q.setParameter("nom",nom);
return (Patient) q.getSingleResult();
}
}
PatientBean:
@ManagedBean
@ViewScoped
public class PatientBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
ConsulterDossierInter consult_dossier;
private Patient patient= new Patient();
private List<Patient> list_patient=new ArrayList<Patient>();
public List<Patient> listePatient(){
list_patient=consult_dossier.getAllPatients();
return list_patient;
}
@PostConstruct
public void init(){
list_patient=consult_dossier.getAllPatients();
}
public PatientBean() {
// TODO Auto-generated constructor stub
}
public ConsulterDossierInter getConsult_dossier() {
return consult_dossier;
}
public void setConsult_dossier(ConsulterDossierInter consult_dossier) {
this.consult_dossier = consult_dossier;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public List<Patient> getList_patient() {
return list_patient;
}
public void setList_patient(List<Patient> list_patient) {
this.list_patient = list_patient;
}
}
最后是xhtml接口index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>
test
</title>
</h:head>
<h:body>
<h1>List of books</h1>
<h:dataTable id="booklist"
value="#{patientBean.list_patient}"
var="book"
border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{book.idPatient}"/>
</h:column>
</h:dataTable>
<hr/>
<h:outputText value="TP3 from Beginning Java EE 7" style="font-style: italic"/>
</h:body>
</html>
顺便说一下,我使用GlassFish服务器4。
由于
答案 0 :(得分:-2)
1-尝试使用Glassfish Payara(优于gf4) 2-为EJB使用Facade模式(优于@Remote接口) 3-用ejb替换弹簧标签