我已经调试了这个,我可以看到ArrayList正在填充正确的条目,但EL没有拿起列表显示在网页中。 session.setAttribute的确切代码用于之前的工作,但现在它没有。设置了上一页中值的属性,但forEach表不是。在调试中,或者如果按F12,它会显示attributen' propertyListings'数组中有x个对象,但它们是空白的,显示为[,,,,,,]。不知道我搞砸了什么。提前谢谢。
我使用的是NetBeans 8.2,服务器是Tomcat。
THE SERVLET:Searching.java
包ServletTasks;
import Business.Inventory;
import Business.Property;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author ti488678
*/
@WebServlet(name = "Searching", urlPatterns = {"/Searching"})
public class Searching extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
double searchLow = Double.parseDouble(request.getParameter("searchLow"));
double searchHigh = Double.parseDouble(request.getParameter("searchHigh"));
String url = "/index.jsp";
String action = request.getParameter("action");
HttpSession session = request.getSession();
if (action == null) {
action = "search"; // default action
}
// perform action and set URL to appropriate page
if (action.equals("search")) {
url = "/searched.jsp";
Inventory inventory = new Inventory();
inventory.setAllProperties();
session.setAttribute("lowSearch", searchLow);
session.setAttribute("highSearch", searchHigh);
ArrayList<Property> propertyList = inventory.search(searchLow, searchHigh);
session.setAttribute("propertyListings", propertyList);
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
网页:index.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Foreal Estate</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css" />
</head>
<header>
<h2>FoReal Estate</h2>
</header>
<main id="wrapper">
<h4>Search: Enter low and high values to search for a property</h4>
<form action="Searching" method="post">
<input type="hidden" name="action" value="search">
<label for="searchLow">Low Value</label>
<input type="text" name="searchLow" value="">
<br><br>
<label for="searchHigh">High Value</label>
<input type="text" name="searchHigh" value="">
<br><br>
<input type="submit" value="Submit Search">
</form>
<br><br><br><br>
</main>
</html>
网页:searching.jsp
<%--
Document : searched
Created on : Feb 8, 2017, 3:45:56 PM
Author : MyPC
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="styles.css" />
<title>Searching Properties</title>
</head>
<header>
<h2>Search Criteria: $${lowSearch} to $${highSearch}</h2>
</header>
<main id="wrapper">
<h2>Listings:</h2>
<table>
<c:forEach var="property" items="${propertyListings}">
<tr>
<td><c:out value="${property.address}"/></td>
<td><c:out value="${property.price}"/></td>
</tr>
</c:forEach>
</table>
</main>
XML:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
JAVA CLASS:Inventory.java
package Business;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;
public class Inventory implements Serializable{
ArrayList<Property> propertyList = new ArrayList<Property>();
public Inventory() {
}
public void setAllProperties() {
Property p = new Property();
p.setAddress("87 Ford Rd");
p.setPrice(2200000.00);
p.setSquareFeet(2000);
p.setTaxes(7000.00);
p.setYearBuilt(LocalDate.of(1865, Month.APRIL, 15));
p.setListingDate(LocalDate.now().minusYears(2));
addProperty(p);
Property p2 = new Property();
p2.setAddress("36 Grassy Knoll");
p2.setPrice(120000.00);
p2.setSquareFeet(2000);
p2.setTaxes(2200.00);
p2.setYearBuilt(LocalDate.of(1963, Month.NOVEMBER, 22));
p2.setListingDate(LocalDate.now().minusWeeks(2));
addProperty(p2);
Property p3 = new Property();
p3.setAddress("223 Oak St");
p3.setPrice(190000.00);
p3.setSquareFeet(4000);
p3.setTaxes(2600.00);
p3.setYearBuilt(LocalDate.of(1901, Month.SEPTEMBER, 14));
p3.setListingDate(LocalDate.now().minusMonths(2));
addProperty(p3);
Property p4 = new Property();
p4.setAddress("13 Foster Lane");
p4.setPrice(110000.00);
p4.setSquareFeet(2500);
p4.setTaxes(1800.00);
p4.setYearBuilt(LocalDate.of(1981, Month.MARCH, 30));
p4.setListingDate(LocalDate.now().minusDays(2));
addProperty(p4);
Property p5 = new Property();
p5.setAddress("15 Foster Lane");
p5.setPrice(115000.00);
p5.setSquareFeet(2500);
p5.setTaxes(1800.00);
p5.setYearBuilt(LocalDate.of(1981, Month.JUNE, 22));
p5.setListingDate(LocalDate.now().minusDays(2));
addProperty(p5);
Property p6 = new Property();
p6.setAddress("782 Butler Ave");
p6.setPrice(105000.00);
p6.setSquareFeet(1800);
p6.setTaxes(1900.00);
p6.setYearBuilt(LocalDate.of(1999, Month.FEBRUARY, 01));
p6.setListingDate(LocalDate.now().minusDays(2));
addProperty(p6);
Property p7 = new Property();
p7.setAddress("70 Martel Road");
p7.setPrice(285000.00);
p7.setSquareFeet(3500);
p7.setTaxes(2800.00);
p7.setYearBuilt(LocalDate.of(2012, Month.MARCH, 15));
p7.setListingDate(LocalDate.now().minusDays(2));
addProperty(p7);
Property p8 = new Property();
p8.setAddress("40th Sister Brook");
p8.setPrice(450000.00);
p8.setSquareFeet(4400);
p8.setTaxes(3720.00);
p8.setYearBuilt(LocalDate.of(2015, Month.JULY, 01));
p8.setListingDate(LocalDate.now().minusDays(2));
addProperty(p8);
Property p9 = new Property();
p9.setAddress("405 Nebraska Street");
p9.setPrice(108980.00);
p9.setSquareFeet(1500);
p9.setTaxes(1350.00);
p9.setYearBuilt(LocalDate.of(1950, Month.JANUARY, 03));
p9.setListingDate(LocalDate.now().minusDays(2));
addProperty(p9);
Property p10 = new Property();
p10.setAddress("1455 Roca Road");
p10.setPrice(423055.00);
p10.setSquareFeet(3750);
p10.setTaxes(3860.00);
p10.setYearBuilt(LocalDate.of(1995, Month.NOVEMBER, 28));
p10.setListingDate(LocalDate.now().minusDays(2));
addProperty(p10);
}
public ArrayList<Property> getProperties() {
setAllProperties();
return propertyList;
}
public void addProperty(Property property) {
propertyList.add(property);
}
public Property getProperty(int index) {
if (index >= 0) {
return (Property) propertyList.get(index);
} else {
return null;
}
}
public int numberOfProperties() {
return propertyList.size();
}
public ArrayList<Property> search(double lowPrice, double highPrice) {
ArrayList<Property> matchingList = new ArrayList<>();
for(Property p : propertyList) {
if(p.getPrice() >= lowPrice && p.getPrice() <= highPrice) {
matchingList.add(p);
}
}
return matchingList;
}
}
JAVA CLASS:Property.java
package Business;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.Month;
public class Property implements Serializable{
private String address;
private int squareFeet;
private double price;
private LocalDate yearBuilt;
private LocalDate listingDate;
private double taxes;
public Property() {
address = "000 default st";
squareFeet = 128;
price = 2048;
yearBuilt = LocalDate.of(1970, Month.JANUARY, 1);
listingDate = yearBuilt.plusYears(5);
taxes = 16;
}
public Property(String address, int squareFeet,
double price, LocalDate yearBuilt, LocalDate listingDate, double taxes) {
this.address = address;
this.squareFeet = squareFeet;
this.price = price;
this.yearBuilt = yearBuilt;
this.listingDate = listingDate;
this.taxes = taxes;
}
public void setAddress(String address) {
this.address = address;
}
public void setSquareFeet(int squareFeet) {
this.squareFeet = squareFeet;
}
public void setPrice(double price) {
this.price = price;
}
public void setYearBuilt(LocalDate yearBuilt) {
this.yearBuilt = yearBuilt;
}
public void setTaxes(double taxes) {
this.taxes = taxes;
}
public String getAddress() {
return (this.address);
}
public int getSquareFeet() {
return (this.squareFeet);
}
public double getPrice() {
return (this.price);
}
public LocalDate getYearBuilt() {
return (this.yearBuilt);
}
public LocalDate getListingDate() {
return (this.listingDate);
}
public double getTaxes() {
return (this.taxes);
}
public void setListingDate(LocalDate listingDate) {
this.listingDate = listingDate;
}
//Optional complete this method to build a String that contains all of the house's information for easy display
//it would be better to use the EL output each property's properties
public String toString() {
String ifYouBuildIt = "";
return ifYouBuildIt;
}
}