如何在eleaf输入表单中格式化日期和时间?

wd2eg0qa  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(333)

在向springhibernatejava模型中插入值时,它抛出了这个错误
错误
“arrivaltime”字段上的对象“flightplanning”中存在字段错误:拒绝值[15:45];代码[typemismatch.flightplanning.arrivaltime,typemismatch.arrivaltime,typemismatch.java.sql.time,typemismatch];参数[org.springframework.context.support.defaultmessagesourceresolvable:代码[flightplanning.arrivaltime,arrivaltime];参数[];默认消息[到达时间]];默认消息[未能将类型为'java.lang.string'的属性值转换为属性'arrivaltime'所需的类型'java.sql.time';嵌套异常为org.springframework.core.convert.conversionfailedexception:未能将值“15:45”从类型[java.lang.string]转换为类型[@javax.persistence.column java.sql.time];嵌套异常是java.lang.illegalargumentexception]字段“deptime”上的对象“flightplanning”中的字段错误:拒绝值[04:56];代码[typemismatch.flightplanning.deptime,typemismatch.deptime,typemismatch.java.sql.time,typemismatch];参数[org.springframework.context.support.defaultmessagesourceresolvable:代码[flightplanning.deptime,deptime];参数[];默认消息[deptime]];默认消息[未能将类型为'java.lang.string'的属性值转换为属性'deptime'所需的类型'java.sql.time';嵌套异常为org.springframework.core.convert.conversionfailedexception:未能从类型转换
这是百里香的形式

<form action="#" th:action="@{/flight-plan-create}"
            th:object="${flightplanning}" method="POST">
            <table
                class="table table-bordered table-striped table-hover table-condensed table-center  text-center "
                id="DyanmicTable">
                <thead>
                    <tr>
                        <th class="text-center">Departure Date</th>
                        <th class="text-center">Source</th>
                        <th class="text-center">Destination</th>
                        <th class="text-center">Departure Time</th>
                        <th class="text-center">Arrival Time</th>
                        <th class="text-center">Offset</th>
                        <th class="text-center">Seat Capacity</th>
                        <th class="text-center">Fare (₹)</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><input type="date" th:field="*{departureDate}"
                            class="form-control"></td>
                        <td><input type="text" th:field="*{departureAirport}"
                            class="form-control"></td>
                        <td><input type="text" th:field="*{arrivalAirport}"
                            class="form-control"></td>
                        <td><input type="time" th:field="*{depTime}"
                            class="form-control"></td>
                        <td><input type="time" th:field="*{arrivalTime}"
                            class="form-control"></td>
                        <td><input type="number" th:field="*{offset}"
                            class="form-control"></td>
                        <td><input type="number" th:field="*{seatCapacity}"
                            class="form-control"></td>
                        <td><input type="number" th:field="*{fare}"
                            class="form-control"></td>
                    </tr>
                </tbody>
            </table>
            <button type="submit" class="center-button btn-primary">Confirm</button>

        </form>

如何格式化*{deptime}和*{arrivaltime},使java.sql.time变量接受这些值。我对spring还不熟悉,不管这是在thymeleaf本身还是在java控制器中完成的。是否有任何方法可以使用spring控制器、thymeleaf或js来完成。
java模型
飞行计划.java

package com.me.springboot.model;

import java.sql.Date;
import java.sql.Time;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "flightplanning")
public class FlightPlanning {
    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY)
    private long flightId;

    @Column(name = "dep_time")
    private Time depTime;

    @Column(name = "arrival_time")
    private Time arrivalTime;

    @Column(name = "offset")
    private int offset;

    @Column(name = "departure_airport")
    private String departureAirport;

    @Column(name = "arrival_airport")
    private String arrivalAirport;

    @Column(name = "departure_Date")
    private Date departureDate;

    @Column(name = "seat_capacity")
    private int seatCapacity;

    @Column(name = "fare")
    private String fare;

    public long getFlightId() {
        return flightId;
    }

    public void setFlightId(long flightId) {
        this.flightId = flightId;
    }

    public Time getDepTime() {
        return depTime;
    }

    public void setDepTime(Time depTime) {
        this.depTime = depTime;
    }

    public Time getArrivalTime() {
        return arrivalTime;
    }

    public void setArrivalTime(Time arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public String getDepartureAirport() {
        return departureAirport;
    }

    public void setDepartureAirport(String departureAirport) {
        this.departureAirport = departureAirport;
    }

    public String getArrivalAirport() {
        return arrivalAirport;
    }

    public void setArrivalAirport(String arrivalAirport) {
        this.arrivalAirport = arrivalAirport;
    }

    public int getSeatCapacity() {
        return seatCapacity;
    }

    public void setSeatCapacity(int seatCapacity) {
        this.seatCapacity = seatCapacity;
    }

    public String getFare() {
        return fare;
    }

    public void setFare(String fare) {
        this.fare = fare;
    }

    public Date getDepartureDate() {
        return departureDate;
    }

    public void setDepartureDate(Date departureDate) {
        this.departureDate = departureDate;
    }

}

flightplanningcontroller.java文件

@GetMapping("/flight-plan/controller")
public String showNewFlightPlanningForm(Model model) {
    FlightPlanning flightplanning = new  FlightPlanning();
    model.addAttribute("flightplanning", flightplanning);
    return "admin/newSchedule";
}

@PostMapping("/flight-plan-create")
public String saveFlightPlanning(@ModelAttribute("flightPlanning") FlightPlanning flightPlanning ) {
    flightPlanningService.saveFlightPlanning(flightPlanning);
    return "redirect:/admin/shedule";
}
gt0wga4j

gt0wga4j1#

问题在于试图储存 Time 在sql中。看起来您的jpa实现不喜欢 sql.Time (我不确定是否有jpa实现可以处理它)试试看 LocalDateTime 它应该适当地持续下去。
编辑:看起来像 sql.Time 也很好(抱歉)
这似乎比我想象的要复杂。看起来你正在尝试输入 String 从表单中获取数据并将其发送到@postmapping。这就是为什么它抱怨 LocalDateTime 转换(它不会像简单的对象类型那样自动转换。你需要找到一种方法来解析 String 将数据放入 LocalDateTime .
一个更简单的解决方法可能是拥有一个预先存在的 FlightPlanning 您可以使用窗体选择。

相关问题