thymelaf模板在chrome中发送请求时没有得到所有响应,我正在从数据库接收数据.绑定中有任何错误吗



飞行

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;
    }
    
}

抽象实体

包com.shahbaz.flightreservation.intities;

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);
    
    
}

预留控制器

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";
    }
    
}

完成保存

<!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>

调试代码时,我正在从数据库中获取值,但在chrome中发送请求时,我没有获取所有值。我只得到航空公司的价值。如果有人能帮我找出我尝试过但无法找到解决方案的错误,因为我是thymelaf的新手

您正在将flights键下的flight实例添加到模型中,而在Thymelaf模板中,您使用${flight....}

更改此项:

modelMap.addAttribute("flights", flight);

modelMap.addAttribute("flight", flight);

应该会有所帮助。

最新更新