【发布时间】:2020-03-12 03:03:08
【问题描述】:
我有一个要循环遍历的列表。本质上,我有一个 XML 文件,上面有一些航班。我要做的是查看用户搜索的内容是否在 XML 文件中,如果是,则在 JSP 页面上将其显示回给他们。发生的情况是,只有我的 XML 中的第一个元素存储在变量中。我需要能够查看整个 XML 文件并查看用户输入是否存在。例如,根据我目前的情况,如果我使用if (startLocation.equals(flight.getStartLocation()) && endLocation.equals(flight.getEndLocation())),它将只返回加利福尼亚州洛杉矶的 startLocation 和新罕布什尔州曼彻斯特的 endLocation。如果用户切换选择框中的内容,则不起作用;正如我所说,它只是收集第一个飞行元素。
FlightController.Java
@RequestMapping(value = "/submit-search", method = RequestMethod.POST)
public String submitSearch(@RequestParam("name") String name, @RequestParam("startLocation") String startLocation, @RequestParam("endLocation") String endLocation,
@RequestParam("date") String date, @RequestParam("passengers") Integer passengers, Model model) throws JAXBException, IOException {
generateFlights();
File file = ResourceUtils.getFile("classpath:flights.xml");
try {
JAXBContext context = JAXBContext.newInstance(FlightList.class);
Unmarshaller un = context.createUnmarshaller();
FlightList flightList = (FlightList) un.unmarshal(file);
List < Flight > list = flightList.getFlights();
for (Flight flight: list) {
if (startLocation.equals(flight.getStartLocation()) && endLocation.equals(flight.getEndLocation()))
{
model.addAttribute("startLocation", flight.getStartLocation());
model.addAttribute("endLocation", flight.getEndLocation());
model.addAttribute("date", flight.getDate());
return "ticketInfo";
}
else {
// Does not exist
return "invalidTicket";
}
}
} catch (JAXBException e) {
model.addAttribute("error", e.toString());
return "error";
}
return null;
}
Flight.Java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="flight")
@XmlType(propOrder = { "startLocation", "endLocation", "date" })
public class Flight {
// Create variables
String startLocation;
String endLocation;
String date;
// Getters and setters for Flight class
@XmlElement
public String getStartLocation() {
return startLocation;
}
public void setStartLocation(String startLocation) {
this.startLocation = startLocation;
}
@XmlElement
public String getEndLocation() {
return endLocation;
}
public void setEndLocation(String endLocation) {
this.endLocation = endLocation;
}
@XmlElement
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
FlightList.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightList {
// XmlElement sets the name of the entities
@XmlElement(name = "flight")
private List<Flight> flights;
public void setFlights(List<Flight> flights) {
this.flights = flights;
}
public List<Flight> getFlights() {
return flights;
}
}
航班.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<flightList>
<flight>
<startLocation>Los Angeles, CA</startLocation>
<endLocation>Manchester, NH</endLocation>
<date>03-22-2020</date>
</flight>
<flight>
<startLocation>Boston, MA</startLocation>
<endLocation>Albany, NY</endLocation>
<date>03-20-2020</date>
</flight>
</flightList>
【问题讨论】:
-
startLocation.equals(flight.getStartLocation()) && endLocation.equals(flight.getEndLocation())这属于 Flight 类的域,应该在这个类中(可能是equals的实现,或者只是isSameConnection(Flight other)或其他东西。