创建一个Web应用程序,通过加载csv,读取数据并将其存储在DAO中来搜索和排序Arraylists。出于某种原因,当我试图测试数据是否可以在JSP上检索和显示时。
MajorImportersbycity.csv
"CITY-VILLE","COMPANY-ENTREPRISE","PROVINCE_ENG","PROVINCE_FRA","POSTAL_CODE-CODE_POSTAL","DATA_YEAR-ANNÉE_DES_DONNÉES"
"100 Mile House","BRAD PADDISON CONTRACTING LTD","British Columbia","Colombie-Britannique","V0K 2E0",2014
"100 Mile House","CENTRAL CHEVROLET GMC BUICK LTD.","British Columbia","Colombie-Britannique","V0K 2E0",2014
"100 Mile House","LONE BUTTE SUPPLY LTD.","British Columbia","Colombie-Britannique","V0K 2E0",2014
"100 Mile House","SARVAIR AVIATION LTD.","British Columbia","Colombie-Britannique","V0K 2E0",2014
"Abbotsford","4 SEASON KING MUSHROOMS LTD.","British Columbia","Colombie-Britannique","V4X 1C9",2014
"Abbotsford","A & P FRUIT GROWERS LTD.","British Columbia","Colombie-Britannique","V4X 2M4",2014
"Abbotsford","ABBOTSFORD CONCRETE PRODUCTS LIMITED PARTNERSHIP","British Columbia","Colombie-Britannique","V2S 7W8",2014
....
Listener.java:
public class Listener implements javax.servlet.ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent context) {
try {
LoadCsvData.importersByCity();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
}
监听器调用LoadCsvData从csv加载数据,解析它并存储到全局arraylist中以用作全局arraylist(填充来自csv的数据),LoadCsvData.java:
public class LoadCsvData {
public static ArrayList<Importer> importersByCanCity;
public static ArrayList<Importer> getImportersByCanCity() {
return importersByCanCity;
}
public static void setImportersByCanCity(ArrayList<Importer> importersByCity) {
importersByCanCity = importersByCity;
}
public static ArrayList<HSCode> productHSCode;
public static void importersByCity() throws FileNotFoundException{
try {
ArrayList<Importer> importersByCity = new ArrayList<Importer>();
String data;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream url = classLoader.getResourceAsStream("/MajorImportersbycity.csv");
BufferedReader csv = new BufferedReader(new InputStreamReader(url));
String line = csv.readLine();
System.out.println("before while");
while ((line = csv.readLine()) != null){
String[] importer = line.split(",");
String city = importer[0];
String companyName = importer[1];
String province_eng = importer[2];
String province_fr = importer[3];
String postal_code = importer[4];
String year = importer[5].toString();
Importer importers = new Importer(city, companyName, province_eng, province_fr, postal_code);
if (importer != null) {
//System.out.println(importer.toString() + "in while");
importersByCity.add(importers);
}
}
csv.close();
System.out.println(importersByCity.size());
setImportersByCanCity(importersByCity);
} catch (IOException f ) {
System.out.println("in catch clause");
f.printStackTrace();
}
}
}
创建了一个用作泛型的类,将csv数据加载到对象中,Importer.java:
public class Importer {
private String city;
private String companyName;
private String province_eng;
private String province_fr;
private String postal_code;
public Importer(String city, String companyName, String province_eng, String province_fr, String postal_code) {
this.city = city;
this.companyName = companyName;
this.province_eng = province_eng;
this.province_fr = province_fr;
this.postal_code = postal_code;
}
public String getCity() {
return city;
}
private void setCity(String city) {
this.city = city;
}
public String getCompanyName() {
return companyName;
}
private void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getProvince_eng() {
return province_eng;
}
private void setProvince_eng(String province_eng) {
this.province_eng = province_eng;
}
public String getProvince_fr() {
return province_fr;
}
private void setProvince_fr(String province_fr) {
this.province_fr = province_fr;
}
public String getPostal_code() {
return postal_code;
}
public void setPostal_code(String postal_code) {
this.postal_code = postal_code;
}
@Override
public String toString(){
return "Importer [name =" + getCompanyName() + ", province=" + getProvince_eng() + " city=" + getCity() + " postal code=" + getPostal_code() + "]";
}
}
我创建了一个测试类作为servlet来测试我现在是否可以在我的jsp,Test.java中使用我加载的arraylist:
public class Test extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
System.out.println("gets to test servlet");
List<Importer> beans = LoadCsvData.getImportersByCanCity();
request.setAttribute("beans", beans);
request.getRequestDispatcher("index.jsp").forward(request, response);
System.out.println(beans.size());
} catch (Throwable theException) {
System.out.println(theException);
}
}
}
我的jsp加载,但它只加载表头。该表不填充CSV,index.jsp:
中的数据<%@ page import="com.importersconnect.importersdata.Importer"
import="com.importersconnect.listener.LoadCsvData" language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Importers Connect</title>
</head>
<body>
<h1>Importers List</h1>
<center>
<table width="100 % " id='table1' border="1" cellspacing="2"
cellpadding="2">
<tr class="tab-highlighted-2">
<td class="tab-highlighted-2" width="15">
<div align="left">city</div>
</td>
<td class="tab-highlighted-2" width="20">
<div align="left">companyName</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">province_eng</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">province_fr</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">postal_code</div>
</td>
</tr>
<c:forEach items="${beans}" var="view">
<tr>
<td><c:out value="${view.city}" /></td>
<td><c:out value="${view.companyName}" /></td>
<td><c:out value="${view.province_eng}" /></td>
<td><c:out value="${view.province_fr}" /></td>
<td><c:out value="${view.postal_code}" /></td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ImportConnect</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.importersconnect.listener.Listener</listener-class>
</listener>
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.importersconnect.sort.Test</servlet-class>
</servlet>
</web-app>