У меня есть объект резервирования как:
package com.nischal.flightreservation.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "RESERVATION")
public class Reservation extends AbstractEntity{
@Column(name = "checked_in")
private Boolean checkedIn;
@Column(name = "no_of_bags")
private int noOfBags;
@OneToOne
private Passenger passenger;
@OneToOne
private Flight flight;
public Boolean getCheckedIn() {
return checkedIn;
}
public void setCheckedIn(Boolean checkedIn) {
this.checkedIn = checkedIn;
}
public int getNoOfBags() {
return noOfBags;
}
public void setNoOfBags(int noOfBags) {
this.noOfBags = noOfBags;
}
public Passenger getPassenger() {
return passenger;
}
public void setPassenger(Passenger passenger) {
this.passenger = passenger;
}
public Flight getFlight() {
return flight;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
@Override
public String toString() {
return "Reservation [checkedIn=" + checkedIn + ", noOfBags=" + noOfBags + ", passenger="
+ passenger + ", flight=" + flight + "]";
}
}
У меня есть класс абстрактного объекта как:
package com.nischal.flightreservation.entities;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public class AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
У меня FlightController есть:
package com.nischal.flightreservation.controllers;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.nischal.flightreservation.entities.Flight;
import com.nischal.flightreservation.repos.FlightRepository;
@Controller
public class FlightController {
@Autowired
private FlightRepository flightRepository;
@RequestMapping("/findFlights")
public String findFlights(@RequestParam(name = "from") String from, @RequestParam(name = "to") String to,@RequestParam(name = "departureDate") @DateTimeFormat(pattern = "dd-MM-yyyy") Date departureDate ,ModelMap modelMap) {
List<Flight> flights = flightRepository.findFlights(from, to, departureDate);
modelMap.addAttribute("flights",flights);
return "displayFlights";
}
}
У меня есть «displayFlights. jsp», который отвечает за отображение всех доступных рейсов и работает нормально.
<table>
<tr>
<th>Airlines</th>
<th>Departure City</th>
<th>Arrival City</th>
<th>Departure Time</th>
</tr>
<c:forEach items = "${flights}" var = "flight">
<tr>
<td>${ flight.operatingAirlines}</td>
<td>${ flight.departureCity}</td>
<td>${ flight.arrivalCity}</td>
<td>${ flight.estimatedDepartureTime}</td>
<td><a href = "showCompleteReservation?flightId=${flight.id }">Select</a>
</tr>
</c:forEach>
</table>
У меня ReservationController как:
package com.nischal.flightreservation.controllers;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.nischal.flightreservation.dto.ReservationRequest;
import com.nischal.flightreservation.entities.Flight;
import com.nischal.flightreservation.entities.Reservation;
import com.nischal.flightreservation.repos.FlightRepository;
import com.nischal.flightreservation.services.ReservationService;
@Controller
public class ReservationController {
@Autowired
FlightRepository flightRepository;
@Autowired
ReservationService reservationService;
@RequestMapping("/showCompleteReservation")
public String showCompleteReservation(@RequestParam("flightId") int flightId,ModelMap
modelMap) {
Flight flight = flightRepository.findById((long) flightId).orElse(null);
modelMap.addAttribute("flight",flight);
return "completeReservation";
}
@RequestMapping(value = "/completeReservation", method = RequestMethod.POST)
public String completeReservation(ReservationRequest request,
@Valid Reservation reservation, BindingResult bindingResult,ModelMap modelMap) {
if(bindingResult.hasErrors()) {
return "completeReservation";
}
else {
modelMap.addAttribute("msg", "Reservation Saved Successfully. "+reservation.getId());
return "reservationConfirmation";
}
}
}
и completeReservation. jsp имеет вид:
<code><html>
<head>
<meta charset="UTF-8">
<title>Reservation Info</title>
</head>
<body>
Airline: ${flight.operatingAirlines }<br>
Departure City: ${flight.operatingAirlines }<br>
Arrival City: ${flight.arrivalCity }<br>
<form action = "completeReservation" method = "post">
<pre>
<h2>Passenger Details</h2>
First Name: <input type = "text" name = "passengerFirstName"/>
Middle Name: <input type = "text" name = "passengerMiddleName"/>
Last Name: <input type = "text" name = "passengerLastName"/>
Email: <input type = "email" name = "passengerEmail"/>
Phone: <input type = "text" name = "passengerPhone"/>
<h2>Card Details</h2>
Name on the Card: <input type = "text" name = "nameOnTheCard"/>
Card Number: <input type = "text" name = "cardNumber"/>
Expiration Date: <input type = "text" name = "expirationDate"/>
Three Digit Sec Code: <input type = "text" name = "threeDigitSecCode"/>
<input type = "hidden" name = "flightId" value = "${flight.id }"/>
<input type = "Submit" value = "Confirm"/>
Но вместо отображения названия действующих авиакомпаний, города вылета, города прибытия ничего не отображается. Я приложил фотографию здесь как:
и всякий раз, когда я нажимаю на кнопку подтверждения. Сообщение о резервировании отображается как:
Я хочу, чтобы идентификатор резервирования был там, а не ноль.