0

My Flight Code:

import java.sql.Timestamp;

import java.util.Date;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

@Entity

@Table(name = "FLIGHT")

public class Flight
{

@Column(name = "flight_number")

private String flightNumber;

@Column(name = "operating_airlines")

private String operatingAirlines;

@Column(name = "arrival_city")

private String arrivalCity;

@Column(name = "departure_city")

private String departureCity;

@Column(name = "date_of_departure")

@DateTimeFormat(pattern = "dd-MM-yyyy")

private Date dateOfDeparture;

@Column(name = "estimated_departure_time")

private Timestamp estimatedDepartureTime;

and contains getters and setters for this:

and In my Flight Repository(Interface) I have:

package com.nischal.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 com.nischal.flightreservation.entities.Flight;


public interface FlightRepository extends JpaRepository<Flight, Integer> {

@Query(value = "select * from FLIGHT where departure_city=:departureCity and 
arrival_city=:arrivalCity and date_of_departure=:dateOfDeparture", nativeQuery = true)

List<Flight> findFlights(@Param("departureCity") String from, @Param("arrivalCity") String 
to, @Param("dateOfDeparture") Date departureDate);

}

and they are correctly mapped with the database tables.

and in my FlightControllers I have:

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.nischal.flightreservation.entities.Flight;

import com.nischal.flightreservation.repos.FlightRepository;

@Controller

public class FlightController {

@Autowired

private FlightRepository flightRepository;

@RequestMapping("/findFlights")

public String findFlights(@RequestParam(required = false,name = "from") String from,
@RequestParam(required = false,name = "to") String to,@RequestParam(required = false, name = 
"departureDate") @DateTimeFormat(pattern = "dd-MM-yyyy") Date departureDate ,ModelMap 
modelMap) 

{

List<Flight> flights = flightRepository.findFlights(from, to, departureDate);

modelMap.addAttribute("flights",flights);

return "displayFlights";

}

}

In my findFlights.jsp I have following code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Search the Flights</title>
</head>
<body>
<form action = "findFlights" method = "post">
    <pre>
        From: <input type = "text" name = "from"/>
        To  : <input type = "text" name = "to"/>
        Departure Date:<input type = "date" name = "departureDate"/>
        <input type = "submit" value = "Search"/>
    </pre>
</form>

and displayFlights is responsible to display the available flights based on the search and the code is :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Available Flights</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?flightId=${flight.id }">Select</a>
    </tr>
    </c:forEach>

</table>

But whenever i hit Search button it yields errors saying:

The Error on webpage is as:

If anyone wants more detail then i can also provide the github link of this project.

N.Neupane
  • 281
  • 1
  • 4
  • 17
  • Maybe this format pattern is wrong? `pattern = "dd-MM-yyyy"` (in FlightControllers) Also there is this (in class `Flight`): `@DateTimeFormat(pattern = "dd-MM-yyyy")` – Abra Apr 11 '20 at 04:49
  • No. Abra I've tried it. Any more suggestions will be helpful. – N.Neupane Apr 11 '20 at 04:57
  • More detail? I’d seriously prefer to have less. [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ole V.V. Apr 12 '20 at 13:45

3 Answers3

0

In request parameter you will get string you need a formatter to convert it to date you can use DateTimeFormatter

@PostMapping("/date")
public void date(@RequestParam("date") 
  @DateTimeFormat(pattern = "dd.MM.yyyy") Date date) {
    // ...You need to map your java date to SQL date 

Or you can create a formatter class and use it.

After process

In hibernate there is temporal annotation to do it Here is a sample

@Temporal(TemporalType.DATE)
private java.util.Date creationDate
0

I can read in the error page "for value '2020-02-02'....".

You have set correctly the pattern in the POJO with the annotation @DateTimeFormat(pattern = "dd-MM-yyyy"). But, by default, JSP are using "yyyy-MM-dd". Here you can read about that is impossible to change the format.

I think that the input format in html will depends on local language browser configuration.

If you use the @Temporal annotation, like Karanjot said, it will map automatically java date to sql date. But the default format still being yyyy-MM-dd. You need both: temporal and format annotations.

The problem is that, in your "front", you are using "yyyy-MM-dd" and your findFlights method expect "dd-MM-yyyy".

I have observe that you are calling the repository directly from controller. Bad practice. Create a service between controller and repository. Your controller will receive "yyyy-MM-dd". Convert it and send to service with correct format.

And to avoid too much arguments to findFlights method (and another), consider to use objects and DTOs.

UrbanoJVR
  • 1,010
  • 1
  • 7
  • 11
0

I have to import two Classes namely Temporal and TemporalType in Model class Flight. Here, is the complete solution.

import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Temporal(TemporalType.DATE)
private Date dateOfDeparture;
N.Neupane
  • 281
  • 1
  • 4
  • 17