所需的长参数 ‘flightId’ 不存在。

huangapple go评论95阅读模式
英文:

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 &#39;flightId&#39; is not present]***      
*Any idea what is wrong here?* I check all files thoroughly but couldn&#39;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(&quot;findFlights&quot;)
    	public String findFlights(@RequestParam(&quot;from&quot;) String from, @RequestParam(&quot;to&quot;) String to, 
    			@RequestParam(&quot;departureDate&quot;) @DateTimeFormat(pattern=&quot;dd-MM-yyyy&quot;) Date departureDate, 
    			ModelMap modelMap) {
    		
    		List&lt;Flight&gt; flights = flightRepository.findFlights(from,to,departureDate);
    		modelMap.addAttribute(&quot;flights&quot;, flights);
    		return &quot;displayFlights&quot;;
    	}
    
    }
    
    
    ----------------------------
    
    **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(&quot;/showCompleteReservation&quot;)
    	public String showCompleteReservation(@RequestParam (&quot;flightId&quot;) Long flightId,ModelMap modelMap) {
    		Flight flight= flightRepository.getOne(flightId);
    				modelMap.addAttribute(&quot;flight&quot;, flight);
    		return &quot;completeReservation&quot;;
    	
    	}
    	
    }
    
    
    ------------------------------------------
    
    **Display Flights jsp**
    
    
    
    
    
    &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    	pageEncoding=&quot;UTF-8&quot;%&gt;
    &lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;
    &lt;%@page isELIgnored=&quot;false&quot;%&gt;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Flight Details&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h2&gt;Flights:&lt;/h2&gt;
    &lt;table&gt;
    &lt;tr&gt;
    &lt;th&gt;Airlines&lt;/th&gt;
    &lt;th&gt;Departure City&lt;/th&gt;
    &lt;th&gt;Arrival City&lt;/th&gt;
    &lt;th&gt;Departure Time&lt;/th&gt;
    &lt;/tr&gt;
    &lt;c:forEach items=&quot;${flights}&quot; var=&quot;flight&quot;&gt;
    &lt;tr&gt;
    &lt;td&gt;${flight.operatingAirlines}&lt;/td&gt;
    &lt;td&gt;${flight.departureCity}&lt;/td&gt;
    &lt;td&gt;${flight.arrivalCity}&lt;/td&gt;
    &lt;td&gt;${flight.estimatedDepartureTime}&lt;/td&gt;
    &lt;td&gt;&lt;a href=&quot;showCompleteReservation?id=${flight.id}&quot;&gt;Select&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    
    &lt;/c:forEach&gt;
    &lt;/table&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    
    
    -------------------------------------------
    
    **Complete Reservation jsp**
    
    
    
    
    &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    	pageEncoding=&quot;UTF-8&quot;%&gt;
    &lt;%@taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;
    &lt;%@page isELIgnored=&quot;false&quot;%&gt;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Complete Reservation&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    
    &lt;h2&gt;Complete Reservation&lt;/h2&gt;
    Airline: ${flight.operatingAirlines}&lt;br/&gt;
    Airline: ${flight.departureCity}&lt;br/&gt;
    Airline: ${flight.arrivalCity}&lt;br/&gt;
    
    &lt;form action=&quot;completeReservation&quot; method=&quot;post&quot;&gt;
    &lt;pre&gt;
    &lt;h2&gt;Passenger Details:&lt;/h2&gt;
    First Name:&lt;input type=&quot;text&quot; name=&quot;passengerFirstName&quot;/&gt;
    Last Name:&lt;input type=&quot;text&quot; name=&quot;passengerLastName&quot;/&gt;
    Email:&lt;input type=&quot;text&quot; name=&quot;passengerEmail&quot;/&gt;
    Phone:&lt;input type=&quot;text&quot; name=&quot;passengerPhone&quot;/&gt;
    &lt;h2&gt;Card Details:&lt;/h2&gt;
    
    Name on the Card:&lt;input type=&quot;text&quot; name=&quot;nameOnTheCard&quot;/&gt;
    Card Number:&lt;input type=&quot;text&quot; name=&quot;cardNumber&quot;/&gt;
    Expiry Date:&lt;input type=&quot;text&quot; name=&quot;expirationDate&quot;/&gt;
    Three Digit Sec Code:&lt;input type=&quot;text&quot; name=&quot;securityCode&quot;/&gt;
    
    &lt;input type=&quot;hidden&quot; name=&quot;flightId&quot; value=&quot;${flight.id}&quot;/&gt;
    &lt;input type=&quot;submit&quot; value=&quot;confirm&quot;/&gt;
    &lt;/pre&gt;
    &lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    
    
    ------------------------------
    
    **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 &quot;Flight [flightNumber=&quot; + flightNumber + &quot;, operatingAirlines=&quot; + operatingAirlines + &quot;, departureCity=&quot;
    				+ departureCity + &quot;, arrivalCity=&quot; + arrivalCity + &quot;, dateOfDeparture=&quot; + dateOfDeparture
    				+ &quot;, estimatedDepartureTime=&quot; + estimatedDepartureTime + &quot;]&quot;;
    	}
    
    }
    
    
    ----------------------------------------
    
    **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 &quot;Reservation [checkedIn=&quot; + checkedIn + &quot;, numberOfBags=&quot; + numberOfBags + &quot;, passenger=&quot; + passenger
    				+ &quot;, flight=&quot; + flight + &quot;]&quot;;
    	}
    
    }



</details>


# 答案1
**得分**: 1

你使用 `id` 请求参数调用你的端点

    &lt;td&gt;&lt;a href=&quot;showCompleteReservation?id=${flight.id}&quot;&gt;选择&lt;/a&gt;&lt;/td&gt;

但是在你的控制器中我们可以看到 `flightId`

    public String showCompleteReservation(@RequestParam (&quot;flightId&quot;) Long flightId)

<details>
<summary>英文:</summary>

You call your endpoint with `id` request param

    &lt;td&gt;&lt;a href=&quot;showCompleteReservation?id=${flight.id}&quot;&gt;Select&lt;/a&gt;&lt;/td&gt;

But in your controller we can see `flightId`


    public String showCompleteReservation(@RequestParam (&quot;flightId&quot;) 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(&quot;/showCompleteReservation&quot;)
public String showCompleteReservation(@RequestParam (&quot;id&quot;) Long id,ModelMap modelMap) {
Flight flight= flightRepository.getOne(id);
modelMap.addAttribute(&quot;flight&quot;, flight);
return &quot;completeReservation&quot;;
}

huangapple
  • 本文由 发表于 2020年7月24日 23:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63077035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定