我无法让jsp tableData遍历列表对象并显示每条记录。
我正在为一个学校项目工作,被困住了。我应该使用Hibernate与SQL数据库(Oracle 11g XE)进行通信,并创建简单的用户界面来管理数据库中的条目。
现在,我能够查询数据库并将所有记录从指定表中获取到ArrayList并通过Java ManagedBean传递。
jsp file:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="datamodel" class="Magazyn.DaneCzesc" scope="session" />
<jsp:setProperty name="datamodel" property="*" />
<!DOCTYPE html>
<html xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wyswietl czesc</title>
</head>
<body>
<h:dataTable value="#{datamodel.tableValues}" var="czesc">
<h:column id="col1" headerClass="color:purple">
<f:facet name="header">ID</f:facet>
<h:outputText value="#{czesc.id_czesc}"/>
</h:column>
<h:column id="col2" headerClass="color:purple">
<f:facet name="header">NAZWA</f:facet>
<h:outputText value="#{czesc.nazwa_czesci}"/>
</h:column>
<h:column id="col3" headerClass="color:purple">
<f:facet name="header">OPIS</f:facet>
<h:outputText value="#{czesc.opis}"/>
</h:column>
<h:column id="col4" headerClass="color:purple">
<f:facet name="header">INFORMACJE DODATKOWE</f:facet>
<h:outputText value="#{czesc.inf_dodatkowe}"/>
</h:column>
<h:column id="col5" headerClass="color:purple">
<f:facet name="header">CZAS OCZEKIWANIA</f:facet>
<h:outputText value="#{czesc.czas_oczekiwania_zamowienie}"/>
</h:column>
<h:column id="col6" headerClass="color:purple">
<f:facet name="header">ILOŚĆ</f:facet>
<h:outputText value="#{czesc.ilosc}"/>
</h:column>
</h:dataTable>
</body>
</html>
Java class:
package Magazyn;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ListDataModel;
import org.hibernate.Query;
import org.hibernate.Session;
@ManagedBean(name="datamodel")
@SessionScoped
public class DaneCzesc extends ListDataModel implements ActionListener{
private Czesc cz;
private MagazynHibernateUtil helper;
private Session session;
private ArrayList <String> lista = new ArrayList<>();
private ArrayList<Czesc> tableValues;
private Integer id_czesc;
public ArrayList<Czesc> getTableValues(){
try{
session = MagazynHibernateUtil.getSessionFactory().openSession();
//org.hibernate.Transaction tx = session.beginTransaction();
//tx.begin();
tableValues = new ArrayList<>();
Query q = session.createQuery("from Czesc");
tableValues = (ArrayList<Czesc>) q.list();
//tx.commit();
session.close();
}
catch (Exception ex){}
return tableValues;
}
public void addCzesc(String a, String b, String c, String d, int e){
cz = new Czesc(a,b,c,d,e);
session = MagazynHibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(cz);
session.getTransaction().commit();
session.close();
}
public String delete(){
return null;
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Apache server log:
19-Jan-2019 00:38:26.178 SEVERE [http-nio-8084-exec-386] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/MagazynSBD] threw exception [An exception occurred processing JSP page /WyswietlCzesc.jsp at line 20
17: <title>Wyswietl czesc</title>
18: </head>
19: <body>
20: <h:dataTable value="#{datamodel.tableValues}" var="czesc">
21:
22: <h:column id="col1" headerClass="color:purple">
23: <f:facet name="header">ID</f:facet>
Stacktrace:] with root cause
java.lang.NullPointerException
at javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1856)
at org.apache.jsp.WyswietlCzesc_jsp._jspx_meth_h_005fdataTable_005f0(WyswietlCzesc_jsp.java:185)
at org.apache.jsp.WyswietlCzesc_jsp._jspService(WyswietlCzesc_jsp.java:148)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
使用tableData是首选方法,但是我可以使用anu other来显示我的数据。