我在jsf项目中有例外,如果你可以帮助我谢谢!遵循以下错误:
javax.servlet.ServletException
javax.faces.webapp.FacesServlet.service(Unknown Source)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
java.lang.NullPointerException
DAO.TestDAO.getPersonne(TestDAO.java:26)
TestMB.TestMB.getAllData(TestMB.java:18)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
javax.el.BeanELResolver.getValue(BeanELResolver.java:94)
com.sun.faces.el.DemuxCompositeELResolver._getValue(Unknown Source)
com.sun.faces.el.DemuxCompositeELResolver.getValue(Unknown Source)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
com.sun.faces.facelets.el.TagValueExpression.getValue(Unknown Source)
javax.faces.component.ComponentStateHelper.eval(Unknown Source)
javax.faces.component.ComponentStateHelper.eval(Unknown Source)
javax.faces.component.UIData.getValue(Unknown Source)
javax.faces.component.UIData.getDataModel(Unknown Source)
javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(Unknown Source)
javax.faces.component.UIData.setRowIndex(Unknown Source)
com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(Unknown Source)
javax.faces.component.UIComponentBase.encodeBegin(Unknown Source)
javax.faces.component.UIData.encodeBegin(Unknown Source)
javax.faces.component.UIComponent.encodeAll(Unknown Source)
javax.faces.component.UIComponent.encodeAll(Unknown Source)
javax.faces.component.UIComponent.encodeAll(Unknown Source)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
javax.faces.webapp.FacesServlet.service(Unknown Source)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
这是我的托管bean类
我把jsf注释
package TestMB;
import DAO.TestDAO;
import Test.Test;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="TestM")
@SessionScoped
public class TestMB {
public List<Test> getAllData (){
return new TestDAO().getPersonne();
}
}
这是我的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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:dataTable value="#{TestM.allData}" var="test">
<h:column>
<f:facet name="nom">
<h:outputText value="#{test.nom}"/>
</f:facet>
</h:column>
<h:column>
<f:facet name="prenom">
<h:outputText value="#{test.prenom}"/>
</f:facet>
</h:column>
</h:dataTable>
</h:body>
</html>
这是我的数据库函数,用于将数据从数据库显示到此xhtml页面:
package DAO;
import Test.Test;
import DAO.DBConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TestDAO {
public TestDAO() {
}
public List<Test> getPersonne() {
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = DBConn.getConnection();
String stm = "Select nom,prenom from t";
List<Test> records = new ArrayList<Test>();
try {
pst = con.prepareStatement(stm);
pst.execute();
rs = pst.getResultSet();
while(rs.next()) {
Test pers = new Test();
pers.setNom(rs.getString(1));
pers.setPrenom(rs.getString(2));
records.add(pers);
}
} catch (SQLException e) {
e.printStackTrace();
}
return records;
}
}
感谢您提供帮助,如果可能的话! :d