百里香未绑定的Spring表单没有正确的权限,抛出异常:参数为空

时间:2019-06-27 13:09:45

标签: spring spring-boot spring-mvc kotlin thymeleaf

我有两种视图,一种用于预定约会,一种用于在日历视图中显示约会。预订成功后,将显示确认信息。确认然后转发到日历视图。我想将预订信息作为参数传递给日历视图,以便它可以在日历中相应地显示新的预订,但是在该步骤中抛出了空异常。

我复制了工作表格的模板。我检查了Web请求,所有必要的数据都在那儿,我认为它没有约束力。

data class EventAppointmentSearchRequest (val startDateTime: LocalDateTime, val endDateTime: LocalDateTime, val rooms: List<Room>)
    /**
     * Gets called when confirming a booking to add it to the DB.
     */
    @PostMapping("/roomBookingConfirmation")
    fun roomBookingConfirmation(model: Model, @ModelAttribute roomBookingRequest: RoomBookingRequest): String {

        makeBooking(roomBookingRequest)

        val date = roomBookingRequest.datetimeFrom
        val start = roomBookingRequest.datetimeFrom.minusDays(date.dayOfWeek.value.toLong())
        val end = roomBookingRequest.datetimeFrom.plusDays(7 - date.dayOfWeek.value.toLong())

        model.addAttribute("eventAppointmentSearchRequest", EventAppointmentSearchRequest(
                startDateTime = start,
                endDateTime = end,
                rooms = listOf(roomRepository.findByRoomName(roomBookingRequest.roomNr))
        ))
        return "roomBookingConfirmation"
    }
    /**
     * Displays the appointments in the calendar view according to the request
     */
    @PostMapping("/calendarView")
    fun calendarView(model: Model, @ModelAttribute eventAppointmentSearchRequest: EventAppointmentSearchRequest): String {
        // THIS THROWS THE EXCEPTION: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method ...requests.EventAppointmentSearchRequest.<init>, parameter startDateTime
        ...
    }
    <!-- /*@thymesVar id="eventAppointmentSearchRequest" type="de.tudarmstadt.pvw.tulpe.soonToBeArtifactory.requests.EventAppointmentSearchRequest"*/ -->
    <form th:action="@{/calendarView}" method="post" th:object="${eventAppointmentSearchRequest}" id="forwardToCalendar" style="grid-column: span 4">
        <H1 th:text="#{roomBooking.bookingConfirmed}">
            Booking confirmed.
        </H1>
        <div class="links">
            <a href="#" th:text="#{roomBooking.nowRedirecting}" onclick="forwardToCalendar()">Redirecting to
                calendarView in </a> <b id="secondsLeft">7</b>
            <input type="hidden" th:field="${eventAppointmentSearchRequest.startDateTime}" th:name="startDateTime" th:value="${eventAppointmentSearchRequest.startDateTime}">
            <input type="hidden" th:field="${eventAppointmentSearchRequest.endDateTime}" th:name="endDateTime" th:value="${eventAppointmentSearchRequest.endDateTime}">
            <input type="hidden" th:field="${eventAppointmentSearchRequest.rooms}" name="rooms[]" th:each="room: ${eventAppointmentSearchRequest.rooms}" th:value="${room.RoomId}">
        </div>
        ...
    </form>

我希望表单能够正确地绑定,我可以在浏览器的Web检查器中看到所有必要的数据以使用EventAppointmentSearchRequest的构造函数。实际输出是此错误消息:

java.lang.IllegalArgumentException:指定为非null的参数为null:方法... requests.EventAppointmentSearchRequest。,参数startDateTime

1 个答案:

答案 0 :(得分:0)

这样的事情对于两个日期隐藏的输入就足够了:

<input type="hidden" th:field="*{endDateTime}">

对于第三个,th:field优先于name和value属性,如果仔细查看生成的HTML,您会发现每个房间隐藏输入的值都是相同的,它是toString()房间列表。这显然是错误的,您需要指定要提交的Room的每个属性,请看以下文章:https://www.baeldung.com/thymeleaf-list

我看不出在客户端和服务器之间再发送两次预订详细信息的意义,我只是将预订ID传递到日历页面并让其加载所有详细信息...