英文:
Required Long parameter 'flightId' is not present]
问题
  *我面临着意外错误 (type=Bad Request, status=400)。在Spring日志中出现以下消息。 **所需的 Long 参数 'flightId' 不存在]***      
*你知道这里出了什么问题吗?* 我仔细检查了所有文件,但无法弄清楚问题出在哪里。是控制器还是 Jsp?
*以下是所有的 Java 和 Jsp 文件。*
 -----------------------------------------------------------------   
   
    **抽象实体**
   
   
   
   
    package com.bharat.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;
    	}
    
    }
    
   
   
    ------------------------------------
   
    **航班控制器**
   
   
   
   
    package com.bharat.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.bharat.flightreservation.entities.Flight;
    import com.bharat.flightreservation.repos.FlightRepository;
    
    @Controller
    public class FlightController {
    	
    	@Autowired
    	FlightRepository flightRepository;
    	
    	@RequestMapping("findFlights")
    	public String findFlights(@RequestParam("from") String from, @RequestParam("to") String to, 
    			@RequestParam("departureDate") @DateTimeFormat(pattern="dd-MM-yyyy") Date departureDate, 
    			ModelMap modelMap) {
    		
    		List<Flight> flights = flightRepository.findFlights(from,to,departureDate);
    		modelMap.addAttribute("flights", flights);
    		return "displayFlights";
    	}
    
    }
    
   
    ----------------------------
   
    **预订控制器**
   
   
   
   
    package com.bharat.flightreservation.controllers;
    
    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.bharat.flightreservation.entities.Flight;
    import com.bharat.flightreservation.repos.FlightRepository;
    
    @Controller
    public class ReservationController {
    	
    	@Autowired
    	FlightRepository flightRepository;
    
    	@RequestMapping("/showCompleteReservation")
    	public String showCompleteReservation(@RequestParam("flightId") Long flightId,ModelMap modelMap) {
    		Flight flight= flightRepository.getOne(flightId);
    				modelMap.addAttribute("flight", flight);
    		return "completeReservation";
    	
    	}
    	
    }
    
   
    ------------------------------------------
   
    **显示航班 JSP**
   
   
   
   
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@page isELIgnored="false"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>航班详情</title>
    </head>
    <body>
    <h2>航班:</h2>
    <table>
    <tr>
    <th>航空公司</th>
    <th>出发城市</th>
    <th>到达城市</th>
    <th>出发时间</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?id=${flight.id}">选择</a></td>
    </tr>
    
    </c:forEach>
    </table>
    </body>
    </html>
    
    
    -------------------------------------------
   
    **完成预订 JSP**
   
   
   
   
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@page isELIgnored="false"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>完成预订</title>
    </head>
    <body>
    
    <h2>完成预订</h2>
    航空公司:${flight.operatingAirlines}<br/>
    出发城市:${flight.departureCity}<br/>
    到达城市:${flight.arrivalCity}<br/>
    
    <form action="completeReservation" method="post">
    <pre>
    <h2>乘客详情:</h2>
    名字: <input type="text" name="passengerFirstName"/>
    姓氏: <input type="text" name="passengerLastName"/>
    电子邮件: <input type="text" name="passengerEmail"/>
    电话: <input type="text" name="passengerPhone"/>
    <h2>卡片详情:</h2>
    
    卡片姓名: <input type="text" name="nameOnTheCard"/>
    卡号: <input type="text" name="cardNumber"/>
    到期日期: <input type="text" name="expirationDate"/>
    三位安全码: <input type="text" name="securityCode"/>
    
    <input type="hidden" name="flightId" value="${flight.id}"/>
    <input type="submit" value="确认"/>
    </pre>
    </form>
    </body>
    </html>
    
    
    ------------------------------
   
    **航班**
   
   
   
   
    package com.bharat.flightreservation.entities;
    
    import java.sql.Timestamp;
    import java.util.Date;
    
    import javax.persistence.Entity;
    
    
    @Entity
    public class Flight extends AbstractEntity {
    
    	private String flightNumber;
    	private String operatingAirlines;
    	private String departureCity;
    	private String arrivalCity;
    	private Date dateOfDeparture;
    	private Timestamp estimatedDepartureTime;
    
    	
    
    	public String getFlightNumber() {
    		return flightNumber;
    	}
    
    	public void setFlightNumber(String flightNumber) {
    		this.flightNumber =
<details>
<summary>英文:</summary>
  *I am facing There was an unexpected error (type=Bad Request, status=400). error and in spring log below message is appearing. **Required Long parameter 'flightId' is not present]***      
*Any idea what is wrong here?* I check all files thoroughly but couldn't figure out where is problem. In controller or Jsp?
*Below are all java and jsp files.*
        
    
 -----------------------------------------------------------------   
    
    **Abstract Entities**
    
    
    
    
    package com.bharat.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;
    	}
    
    }
    
    
    
    ------------------------------------
    
    **Flight Controller**
    
    
    
    
    package com.bharat.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.bharat.flightreservation.entities.Flight;
    import com.bharat.flightreservation.repos.FlightRepository;
    
    @Controller
    public class FlightController {
    	
    	@Autowired
    	FlightRepository flightRepository;
    	
    	@RequestMapping("findFlights")
    	public String findFlights(@RequestParam("from") String from, @RequestParam("to") String to, 
    			@RequestParam("departureDate") @DateTimeFormat(pattern="dd-MM-yyyy") Date departureDate, 
    			ModelMap modelMap) {
    		
    		List<Flight> flights = flightRepository.findFlights(from,to,departureDate);
    		modelMap.addAttribute("flights", flights);
    		return "displayFlights";
    	}
    
    }
    
    
    ----------------------------
    
    **Reservaction Controller**
    
    
    
    
    package com.bharat.flightreservation.controllers;
    
    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.bharat.flightreservation.entities.Flight;
    import com.bharat.flightreservation.repos.FlightRepository;
    
    @Controller
    public class ReservationController {
    	
    	@Autowired
    	FlightRepository flightRepository;
    
    	@RequestMapping("/showCompleteReservation")
    	public String showCompleteReservation(@RequestParam ("flightId") Long flightId,ModelMap modelMap) {
    		Flight flight= flightRepository.getOne(flightId);
    				modelMap.addAttribute("flight", flight);
    		return "completeReservation";
    	
    	}
    	
    }
    
    
    ------------------------------------------
    
    **Display Flights jsp**
    
    
    
    
    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@page isELIgnored="false"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Flight Details</title>
    </head>
    <body>
    <h2>Flights:</h2>
    <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?id=${flight.id}">Select</a></td>
    </tr>
    
    </c:forEach>
    </table>
    </body>
    </html>
    
    
    -------------------------------------------
    
    **Complete Reservation jsp**
    
    
    
    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@page isELIgnored="false"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Complete Reservation</title>
    </head>
    <body>
    
    <h2>Complete Reservation</h2>
    Airline: ${flight.operatingAirlines}<br/>
    Airline: ${flight.departureCity}<br/>
    Airline: ${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 Number:<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" value="${flight.id}"/>
    <input type="submit" value="confirm"/>
    </pre>
    </form>
    </body>
    </html>
    
    
    ------------------------------
    
    **Flight**
    
    
    
    
    
    package com.bharat.flightreservation.entities;
    
    import java.sql.Timestamp;
    import java.util.Date;
    
    import javax.persistence.Entity;
    
    
    
    @Entity
    public class Flight extends AbstractEntity {
    
    	private String flightNumber;
    	private String operatingAirlines;
    	private String departureCity;
    	private String arrivalCity;
    	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;
    	}
    
    	@Override
    	public String toString() {
    		return "Flight [flightNumber=" + flightNumber + ", operatingAirlines=" + operatingAirlines + ", departureCity="
    				+ departureCity + ", arrivalCity=" + arrivalCity + ", dateOfDeparture=" + dateOfDeparture
    				+ ", estimatedDepartureTime=" + estimatedDepartureTime + "]";
    	}
    
    }
    
    
    ----------------------------------------
    
    **Reservation** 
    
    
    
    
    package com.bharat.flightreservation.entities;
    
    import javax.persistence.Entity;
    import javax.persistence.OneToOne;
    
    @Entity
    public class Reservation extends AbstractEntity {
    
    	private Boolean checkedIn;
    	private int numberOfBags;
    	@OneToOne
    	private Passenger passenger;
    	@OneToOne
    	private Flight flight;
    
    	
    
    	public Boolean getCheckedIn() {
    		return checkedIn;
    	}
    
    	public void setCheckedIn(Boolean checkedIn) {
    		this.checkedIn = checkedIn;
    	}
    
    	public int getNumberOfBags() {
    		return numberOfBags;
    	}
    
    	public void setNumberOfBags(int numberOfBags) {
    		this.numberOfBags = numberOfBags;
    	}
    
    	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 + ", numberOfBags=" + numberOfBags + ", passenger=" + passenger
    				+ ", flight=" + flight + "]";
    	}
    
    }
</details>
# 答案1
**得分**: 1
你使用 `id` 请求参数调用你的端点
    <td><a href="showCompleteReservation?id=${flight.id}">选择</a></td>
但是在你的控制器中我们可以看到 `flightId`
    public String showCompleteReservation(@RequestParam ("flightId") Long flightId)
<details>
<summary>英文:</summary>
You call your endpoint with `id` request param
    <td><a href="showCompleteReservation?id=${flight.id}">Select</a></td>
But in your controller we can see `flightId`
    public String showCompleteReservation(@RequestParam ("flightId") Long flightId)
</details>
# 答案2
**得分**: 0
感谢亲爱的,将方法中的内容更改为使用 id 后,它成功运行了。
```java
@RequestMapping("/showCompleteReservation")
public String showCompleteReservation(@RequestParam("id") Long id, ModelMap modelMap) {
    Flight flight = flightRepository.getOne(id);
    modelMap.addAttribute("flight", flight);
    return "completeReservation";
}
英文:
Thank Dear It worked after changing it to id in method.
@RequestMapping("/showCompleteReservation")
	public String showCompleteReservation(@RequestParam ("id") Long id,ModelMap modelMap) {
		Flight flight= flightRepository.getOne(id);
				modelMap.addAttribute("flight", flight);
		return "completeReservation";
	
	}
专注分享java语言的经验与见解,让所有开发者获益!



评论