如本主题所述,我想使用JSF 2.1数据表显示矩阵。我正在通过互联网查找与我的问题相关的线程,我遇到了一个最相关的线程: http://www.coderanch.com/t/478265/JSF/java/Displaying-array-data-tables
我从它的代码中获得灵感来构建我的,不幸的是,我没有结果(空白页面)。这是我的代码:
1-JSP Page:
<!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:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<ui:composition
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<p:dataTable value="#{testBean.listMonthlySalaryDetail}" id="row" var="row"
paginator="true" rows="15"
paginatorPosition="top" >
<c:forEach var="i" begin="0" end="#{testBean.noOfColumns-1}" step="1">
<p:column headerText="#{testBean.listColumnsInfo.get(i).name}"
style="text-align: #{testBean.findAlign(i)}}">
<h:outputLabel value="#{row.get(i)}"/>
</p:column>
</c:forEach>
</p:dataTable>
</ui:composition>
</h:body>
</html>
2- Java类:
package gourav.garg;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = 1578643110698049445L;
private List<ArrayList<String>> listMonthlySalaryDetail = new ArrayList<ArrayList<String>>();
private List<ColumnInfo> listColumnsInfo = new ArrayList<ColumnInfo>(); ;
private int noOfColumns;
public TestBean() {
this.getQueryResult();
}
public String findAlign(int i) {
if (getListColumnsInfo().get(i).getClassName().trim().equals("java.lang.String")) {
return "left";
} else if (getListColumnsInfo().get(i).getClassName().trim().equals("java.math.BigDecimal")) {
return "right";
}
return "left";
}
public List<ColumnInfo> getListColumnsInfo() {
return listColumnsInfo;
}
public void setListColumnsInfo(List<ColumnInfo> listColumnsInfo) {
this.listColumnsInfo = listColumnsInfo;
}
public int getNoOfColumns() {
return noOfColumns;
}
public void setNoOfColumns(int noOfColumns) {
this.noOfColumns = noOfColumns;
}
public List<ArrayList<String>> getListMonthlySalaryDetail() {
return listMonthlySalaryDetail;
}
public void setListMonthlySalaryDetail(List<ArrayList<String>> listMonthlySalaryDetail) {
this.listMonthlySalaryDetail = listMonthlySalaryDetail;
}
private Connection getConnection() {
Connection connection = null;
try {
String url = "jdbc:mysql://localhost:3306/GESTION_PRODUITS";
String user = "root";
String password = "";
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public void getQueryResult() {
try {
Class.forName("com.mysql.jdbc.Driver");
Statement state = this.getConnection().createStatement();
ResultSet resultSet = state.executeQuery("select * from produits");
this.listColumnsInfo = this.buildColumnsList(resultSet);
this.listMonthlySalaryDetail = this.buildContentMatrix(resultSet);
resultSet.close();
state.close();
System.out.println("Size listMonthlySalaryDetail : " + listMonthlySalaryDetail.size());
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
private List<ColumnInfo> buildColumnsList(ResultSet resultSet) {
List<ColumnInfo> columnsList = new ArrayList<ColumnInfo>();
try {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
setNoOfColumns(resultSetMetaData.getColumnCount());
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++){
String columnLabel = resultSetMetaData.getColumnLabel(i);
String className = resultSetMetaData.getColumnClassName(i);
columnsList.add(new ColumnInfo(i, columnLabel, className));
}
}
catch (SQLException e) {
e.printStackTrace();
}
return columnsList;
}
private List<ArrayList<String>> buildContentMatrix(ResultSet resultSet) {
List<ArrayList<String>> contentMatrix = new ArrayList<ArrayList<String>>();
try {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
for(int i=0; i<resultSetMetaData.getColumnCount(); ++i)
contentMatrix.add(new ArrayList<String>());
while (resultSet.next()) {
for (int columnIndex = 1; columnIndex < resultSetMetaData.getColumnCount(); columnIndex++){
if(resultSet.getObject(columnIndex)==null)
contentMatrix.get(columnIndex).add("");
else
contentMatrix.get(columnIndex).add(resultSet.getObject(columnIndex).toString());
}
}
}
catch (SQLException e) {
e.printStackTrace();
}
return contentMatrix;
}
}
3-日志:
Jul 16, 2014 6:15:11 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Windows PowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\TortoiseHg\;C:\Program Files\MySQL\MySQL Server 5.1\bin;%JAVA_HOME%\bin;C:\Program Files\TortoiseSVN\bin;D:\TAF\ESIR3\Stage\Tests\TestMaven\maven\bin;.
Jul 16, 2014 6:15:11 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:FirstJSF' did not find a matching property.
Jul 16, 2014 6:15:12 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jul 16, 2014 6:15:12 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jul 16, 2014 6:15:12 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2204 ms
Jul 16, 2014 6:15:12 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 16, 2014 6:15:12 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.53
Jul 16, 2014 6:15:14 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [193] milliseconds.
Jul 16, 2014 6:15:18 PM org.apache.myfaces.config.DefaultFacesConfigurationProvider getStandardFacesConfig
INFO: Reading standard config META-INF/standard-faces-config.xml
log4j:WARN No appenders could be found for logger (org.apache.commons.digester.Digester.sax).
log4j:WARN Please initialize the log4j system properly.
Jul 16, 2014 6:15:19 PM org.apache.myfaces.config.DefaultFacesConfigurationProvider getWebAppFacesConfig
INFO: Reading config /WEB-INF/faces-config.xml
Jul 16, 2014 6:15:20 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact
INFO: Artifact 'myfaces-api' was found in version '2.1.5' from path 'file:/D:/TAF/Perso/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapp s/FirstJSF/WEB-INF/lib/myfaces-api-2.1.5.jar'
Jul 16, 2014 6:15:20 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact
INFO: Artifact 'myfaces-impl' was found in version '2.1.5' from path 'file:/D:/TAF/Perso/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/FirstJSF/WEB-INF/lib/myfaces-impl-2.1.5.jar'
Jul 16, 2014 6:15:20 PM org.apache.myfaces.util.ExternalSpecifications isBeanValidationAvailable
INFO: MyFaces Bean Validation support disabled
Jul 16, 2014 6:15:20 PM org.apache.myfaces.application.ApplicationImpl getProjectStage
INFO: Couldn't discover the current project stage, using Production
Jul 16, 2014 6:15:20 PM org.apache.myfaces.config.FacesConfigurator handleSerialFactory
INFO: Serialization provider : class org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory
Jul 16, 2014 6:15:20 PM org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory getLifecycleProvider
INFO: Using LifecycleProvider org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider
Jul 16, 2014 6:15:20 PM org.apache.myfaces.webapp.AbstractFacesInitializer initFaces
INFO: ServletContext initialized.
Jul 16, 2014 6:15:20 PM org.apache.myfaces.webapp.WebConfigParamsLogger logWebContextParams
INFO: Tomahawk jar not available. Autoscrolling, DetectJavascript, AddResourceClass and CheckExtensionsFilter are disabled now.
Jul 16, 2014 6:15:20 PM org.apache.myfaces.webapp.WebConfigParamsLogger logWebContextParams
INFO: Scanning for context init parameters not defined. It is not necessary to define them all into your web.xml, they are just provided here for informative purposes. To disable this messages set org.apache.myfaces.LOG_WEB_CONTEXT_PARAMS config param to 'false'
Jul 16, 2014 6:15:20 PM org.apache.myfaces.webapp.WebConfigParamsLogger logWebContextParams
INFO: No context init parameter 'javax.faces.RESOURCE_EXCLUDES' found, using default value '.class .jsp .jspx .properties .xhtml .groovy'.
4 - 我目前正在使用:
Java 7 - Tomcat 7 - JSF 2.1 - Eclipse Juno
提前感谢您的帮助,
奥马。