datatable 不通过链表从数据库呈现 jsf 中的数据
datatable doesnot render data in jsf from database through linkedlist
这是我的 jsf 页面,我在其中很好地满足了 java bean 对象的属性要求。我已经检查了该网站中的所有问题,但中午似乎有所帮助。我不知道我的代码有什么问题
<h:dataTable value="#{contact.getPeopleList()}" var="data">
<h:column>
<f:facet name="header">ID</f:facet>
<h:outputText value="#{data.id}"/>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{persons.name}"/>
</h:column>
<h:column>
<f:facet name="header">Mobile Number</f:facet>
#{persons.mobilenumber}
</h:column>
<h:column>
<f:facet name="header">Phone Number</f:facet>
#{persons.telephone}
</h:column>
<h:column>
<f:facet name="header">Address</f:facet>
#{persons.address}
</h:column>
</h:dataTable>
</h:body>
这是我的联系人 CLass,我在其中注入了 Person class 以获得 Person class
的属性
@Named
@RequestScoped
public class Contact {
private Person person;
@PostConstruct
public void init(){
this.person=new Person();
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String onSubmit(){
ContactDAO doaContact=new ContactDAO();
doaContact.insertContact(person);
return "contactpage";
}
public List<Person> getPeopleList(){
ContactDAO doaContact=new ContactDAO();
List<Person> personList =doaContact.getAll();
return personList;
}
}
package np.com.drose.beans;
import java.util.List;
import np.com.drose.doa.ContactDAO;
public class Person {
private int id;
private String name;
private int mobilenumber;
private int telephone;
private String address;
public Person() {
}
public Person(int id, String name, int mobilenumber, int telephone, String address) {
this.id = id;
this.name = name;
this.mobilenumber = mobilenumber;
this.telephone = telephone;
this.address = address;
}
public Person(String name, int mobilenumber, int telephone, String address) {
this.name = name;
this.mobilenumber = mobilenumber;
this.telephone = telephone;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMobilenumber() {
return mobilenumber;
}
public void setMobilenumber(int mobilenumber) {
this.mobilenumber = mobilenumber;
}
public int getTelephone() {
return telephone;
}
public void setTelephone(int telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
在这里我对数据访问对象感到困惑 class 我似乎无法找到我做错了什么以及为什么我的代码没有从数据库中呈现数据
public List<Person> getAll(){
List<Person> contactList= new LinkedList<>();
String sql="Select * from contact";
try{
conn = DataConnection.getConnetion();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Person person=new Person(rs.getInt("id"),rs.getString("name"),rs.getInt("mobilenumber"),rs.getInt("telephone"),rs.getString("address"));
contactList.add(person);
}
}catch(Exception EX){
EX.printStackTrace();
}
return contactList;
}
要在 jsf 中呈现数据表,您需要具有 Bean 的 listas 属性 class.For 示例:
@Named
@RequestScoped
public class Contact {
private Person person;
private List<Person>personList;
@PostConstruct
public void init(){
this.person=new Person();
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public List<Person> getPersonList() {
return personList;
}
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
public String onSubmit(){
ContactDAO doaContact=new ContactDAO();
doaContact.insertContact(person);
return "contactpage";
}
public List<Person> getPeopleList(){
ContactDAO doaContact=new ContactDAO();
personList =doaContact.getAll();
return personList;
}
}
并在您的 html 文件中
你应该这样写:
<h:dataTable value="#{contact.personList}" var="data">
请这样检查。
这是我的 jsf 页面,我在其中很好地满足了 java bean 对象的属性要求。我已经检查了该网站中的所有问题,但中午似乎有所帮助。我不知道我的代码有什么问题
<h:dataTable value="#{contact.getPeopleList()}" var="data">
<h:column>
<f:facet name="header">ID</f:facet>
<h:outputText value="#{data.id}"/>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{persons.name}"/>
</h:column>
<h:column>
<f:facet name="header">Mobile Number</f:facet>
#{persons.mobilenumber}
</h:column>
<h:column>
<f:facet name="header">Phone Number</f:facet>
#{persons.telephone}
</h:column>
<h:column>
<f:facet name="header">Address</f:facet>
#{persons.address}
</h:column>
</h:dataTable>
</h:body>
这是我的联系人 CLass,我在其中注入了 Person class 以获得 Person class
的属性 @Named
@RequestScoped
public class Contact {
private Person person;
@PostConstruct
public void init(){
this.person=new Person();
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String onSubmit(){
ContactDAO doaContact=new ContactDAO();
doaContact.insertContact(person);
return "contactpage";
}
public List<Person> getPeopleList(){
ContactDAO doaContact=new ContactDAO();
List<Person> personList =doaContact.getAll();
return personList;
}
}
package np.com.drose.beans;
import java.util.List;
import np.com.drose.doa.ContactDAO;
public class Person {
private int id;
private String name;
private int mobilenumber;
private int telephone;
private String address;
public Person() {
}
public Person(int id, String name, int mobilenumber, int telephone, String address) {
this.id = id;
this.name = name;
this.mobilenumber = mobilenumber;
this.telephone = telephone;
this.address = address;
}
public Person(String name, int mobilenumber, int telephone, String address) {
this.name = name;
this.mobilenumber = mobilenumber;
this.telephone = telephone;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMobilenumber() {
return mobilenumber;
}
public void setMobilenumber(int mobilenumber) {
this.mobilenumber = mobilenumber;
}
public int getTelephone() {
return telephone;
}
public void setTelephone(int telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
在这里我对数据访问对象感到困惑 class 我似乎无法找到我做错了什么以及为什么我的代码没有从数据库中呈现数据
public List<Person> getAll(){
List<Person> contactList= new LinkedList<>();
String sql="Select * from contact";
try{
conn = DataConnection.getConnetion();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Person person=new Person(rs.getInt("id"),rs.getString("name"),rs.getInt("mobilenumber"),rs.getInt("telephone"),rs.getString("address"));
contactList.add(person);
}
}catch(Exception EX){
EX.printStackTrace();
}
return contactList;
}
要在 jsf 中呈现数据表,您需要具有 Bean 的 listas 属性 class.For 示例:
@Named
@RequestScoped
public class Contact {
private Person person;
private List<Person>personList;
@PostConstruct
public void init(){
this.person=new Person();
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public List<Person> getPersonList() {
return personList;
}
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
public String onSubmit(){
ContactDAO doaContact=new ContactDAO();
doaContact.insertContact(person);
return "contactpage";
}
public List<Person> getPeopleList(){
ContactDAO doaContact=new ContactDAO();
personList =doaContact.getAll();
return personList;
}
}
并在您的 html 文件中 你应该这样写:
<h:dataTable value="#{contact.personList}" var="data">
请这样检查。