Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 已解决[org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:2个错误_Java - Fatal编程技术网

Java 已解决[org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:2个错误

Java 已解决[org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:2个错误,java,Java,我在将航班信息保存到mysql数据库时出错。请帮助我 下面是我的代码: 我尝试了所有的方法,添加了图案和 这么多 它不会从字符串转换为日期 控制台日志 2020-05-12 13:19:21.040[0;39m [33m WARN[0;39m [35m5472[0;39m [2m---[0;39m [2m[nio-8080-exec-8][0;39m [36m.w.s.m.s.DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [o

我在将航班信息保存到mysql数据库时出错。请帮助我 下面是我的代码:

我尝试了所有的方法,添加了图案和 这么多

它不会从字符串转换为日期

控制台日志

2020-05-12 13:19:21.040[0;39m [33m WARN[0;39m [35m5472[0;39m [2m---[0;39m [2m[nio-8080-exec-8][0;39m [36m.w.s.m.s.DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'flight' on field 'dateOfDeparture': rejected value [2020-05-20]; codes [typeMismatch.flight.dateOfDeparture,typeMismatch.dateOfDeparture,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [flight.dateOfDeparture,dateOfDeparture]; arguments []; default message [dateOfDeparture]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfDeparture'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-05-20'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'flight' on field 'estimatedDepartureTime': rejected value [2020-05-20]; codes [typeMismatch.flight.estimatedDepartureTime,typeMismatch.estimatedDepartureTime,typeMismatch.java.sql.Timestamp,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [flight.estimatedDepartureTime,estimatedDepartureTime]; arguments []; default message [estimatedDepartureTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'estimatedDepartureTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.sql.Timestamp] for value '2020-05-20'; nested exception is java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]]]
应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/reservation?useSSL=false&serverTimeZone=IST&useLegacyDatetimeCode=false
spring.datasource.username=root
spring.datasource.password=admin

spring.jpa.show-sql=true

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

server.servlet.context-path=/flight-reservation

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=*****
spring.mail.password=SunMoon@123
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true

#Defined in logback.xml
logging.level.root=INFO
logging.file.name=D:/backup-Mahesh-Accion/logs/flight-reservation.log

com.accion.reservation.itenery.dirpath=C:/Users/Desktop/reservation/tickets
com.accion.reservation.itenery.email.body=Please find your attached flight booking tickets and Happy Journey! :)
com.accion.reservation.itenery.email.subject=Flight Booking Tickets Mahesh
FlightController.java

@Controller
public class FlightController {

@Autowired
    private FlightRepository flightRepository;

  @RequestMapping(value = "/saveFlight", method = RequestMethod.POST)
    public String addFlight(@ModelAttribute @DateTimeFormat(pattern = "yyyy-MM-dd") Flight flight ) {
        flightRepository.save(flight);
        return "addFlight";

    }
}
flight.java实体类

Entity
public class Flight  {
private String flightNumber;

  private int id
    private String operatingAirlines;
    private String departureCity;
    private String arrivalCity;

    private Date dateOfDeparture;
    private Timestamp estimatedDepartureTime;

  //setters and getters
FlightRepository.java

@Repository
public interface FlightRepository extends JpaRepository<Flight, Long> {

}
@存储库
公共接口FlightRepository扩展了JpaRepository{
}
addFlight.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add Flight</title>
</head>
<body>
    <h2>Add Flight :</h2>

    <form action="saveFlight"  method="post">

    FlightNumber :<input type="text" name="flightNumber"> </br>
    Airlines Name: <input type="text" name="operatingAirlines"></br>
    Departure City : <input type="text" name="departureCity"></br>
    Arrival City: <input type="text" name="arrivalCity"></br>
    Date Of Departure: <input type="date" name="dateOfDeparture"></br>
    Estimated Date of Departure: <input type="date" name="estimatedDepartureTime"></br> 
    <input type="submit" value="Save Flight"></br>

    </form>
</body>
</html>

添加航班
添加航班:
航班号:
航空公司名称:
出发城市:
抵达城市:
出发日期:
预计出发日期:

航班
类中的日期类型字段上使用
@DateTimeFormat(pattern=“yyy-MM-dd”)
。 使用日期数据时,例如:
2020-05-20
用于
EstimatedPartureTime
使用
date
类型

public class Flight  {
    ...
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date dateOfDeparture;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date estimatedDepartureTime;
}
使用
@DateTimeFormat(pattern=“yyyy-MM-dd”)
航班
类中的日期类型字段上。 使用日期数据时,例如:
2020-05-20
用于
EstimatedPartureTime
使用
date
类型

public class Flight  {
    ...
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date dateOfDeparture;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date estimatedDepartureTime;
}