![](/img/trans.png)
[英]not getting all data updated in database instead i am sending request with all data values
[英]thymeleaf template not getting all response while sending request in chrome, I am receiving data from database is there any error in binding?
航班
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;
}
}
抽象實體
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;
}
}
飛行存儲庫
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 中發送請求時我沒有獲取所有值。我只獲取航空公司值。如果有人可以幫助我找出我嘗試過但無法提出解決方案的錯誤我是 thymeleaf 的新手
您正在將flight
實例添加到 model 的flights
鍵下,而在 Thymeleaf 模板中,您使用${flight....}
。
改變這個:
modelMap.addAttribute("flights", flight);
至
modelMap.addAttribute("flight", flight);
應該有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.