[英]thymeleaf template not getting all response while sending request in chrome, I am receiving data from database is there any error in binding?
Flight航班
package com.shahbaz.flightreservation.entities;
import java.sql.Timestamp;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Flight extends AbstractEntity {
private String flightNumber;
private String operatingAirlines;
private String departureCity;
private String arrivalCity;
@Temporal(TemporalType.DATE)
private Date dateOfDeparture;
private Timestamp estimatedDepartureTime;
public String getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public String getOperatingAirlines() {
return operatingAirlines;
}
public void setOperatingAirlines(String operatingAirlines) {
this.operatingAirlines = operatingAirlines;
}
public String getDepartureCity() {
return departureCity;
}
public void setDepartureCity(String departureCity) {
this.departureCity = departureCity;
}
public String getArrivalCity() {
return arrivalCity;
}
public void setArrivalCity(String arrivalCity) {
this.arrivalCity = arrivalCity;
}
public Date getDateOfDeparture() {
return dateOfDeparture;
}
public void setDateOfDeparture(Date dateOfDeparture) {
this.dateOfDeparture = dateOfDeparture;
}
public Timestamp getEstimatedDepartureTime() {
return estimatedDepartureTime;
}
public void setEstimatedDepartureTime(Timestamp estimatedDepartureTime) {
this.estimatedDepartureTime = estimatedDepartureTime;
}
}
AbstractEntity抽象实体
package com.shahbaz.flightreservation.entities; package com.shahbaz.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.IDENTITY)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
FlightRepository飞行存储库
package com.shahbaz.flightreservation.repos;
import java.util.Date;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository;
import com.shahbaz.flightreservation.entities.Flight;
@Repository
public interface FlightRepository extends JpaRepository<Flight,Long> {
@Query("from Flight where departureCity=:departureCity and arrivalCity=:arrivalCity and dateOfDeparture=:dateOfDeparture")
List<Flight> findFlights(@Param("departureCity") String from, @Param("arrivalCity") String to,
@Param("dateOfDeparture") Date departureDate);
@Query("from Flight where id=:id")
Flight findOne(@Param("id") Long flightId);
}
ReservationController预订控制器
package com.shahbaz.flightreservation.controllers;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
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.shahbaz.flightreservation.entities.Flight;
import com.shahbaz.flightreservation.repos.FlightRepository;
@Controller
public class ReservationController {
@Autowired
FlightRepository flightRepository;
@RequestMapping("/showCompleteReservation")
public String showCompleteReservation(@RequestParam("flightId") Long flightId,ModelMap modelMap)
{
System.out.println("Welcome Home");
Flight flight = flightRepository.findOne(flightId);
modelMap.addAttribute("flights", flight);
System.out.println(flight.getArrivalCity());
System.out.println(flight.getDepartureCity());
return "completeReservation";
}
}
completeReservation完成预订
<:DOCTYPE HTML> <html lang="en" xmlns:th="http.//www.thymeleaf;org"> <head> <meta http-equiv="Content-Type" content="text/html: charset=UTF-8"> <title>Complete Reservation</title> </head> <body> <h2>Complete Reservation</h2> Airline: <span th.text="${flight:operatingAirlines}"> <br/> Departure City: <span th.text="${flight:departureCity}"><br/> Arrival City:<spanp th.text="${flight:arrivalCity}"><br/> <form action="completeReservation" method="post"> <pre> <h2>Passenger Details:</h2> First Name:<input type="text" name="passengerFirstName"/> Last Name:<input type="text" name="passengerLastName"/> Email:<input type="text" name="passengerEmail"/> Phone:<input type="text" name="passengerPhone"/> <h2>Card Details:</h2> Name on the card:<input type="text" name="nameOnTheCard"/> Card No:<input type="text" name="cardNumber"/> Expiry Date:<input type="text" name="expirationDate"/> Three Digit Sec Code:<input type="text" name="securityCode"/> <input type="hidden" name="flightId" th.value="${flight.id}"/> <input type="submit" value="confirm"/> </pre> </form> </body> </html>
While Debugging the code I am Getting Values from database But i am not getting all values while sending request in chrome.I am Getting only Airline value.If anyone can help me to find out the error I tried but unable to come up with solution as i am new to thymeleaf在调试代码时,我正在从数据库中获取值但是在 chrome 中发送请求时我没有获取所有值。我只获取航空公司值。如果有人可以帮助我找出我尝试过但无法提出解决方案的错误我是 thymeleaf 的新手
You are adding your flight
instance under the flights
key to the model while in the Thymeleaf template, you use ${flight....}
.您正在将flight
实例添加到 model 的flights
键下,而在 Thymeleaf 模板中,您使用${flight....}
。
Changing this:改变这个:
modelMap.addAttribute("flights", flight);
to至
modelMap.addAttribute("flight", flight);
should help.应该有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.