eleaf:处理和编辑由引用属性组成的列表

xytpbqjk  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(185)

我现在正在绞尽脑汁研究如何把我和我的室友的名单发到教室里,并在教室里修改它们。
我的roomdto班级定义如下:

@Getter
@Setter
public class RoomDTO {

    long roomNumber;
    String roomCategory;
    //LocaleDate as String as key
    Map<String, String> roomStateDays;

    public RoomDTO(){

    }

    public static RoomDTO fromRoom(Room room) {
        RoomDTO roomDTO = new RoomDTO();
        roomDTO.roomCategory = room.roomCategory().categoryName();
        roomDTO.roomNumber = room.roomNumber();

        roomDTO.roomStateDays = room.roomStateDays()
                .stream()
                .collect(Collectors.toMap(
                        (RoomStateDay rsd ) -> rsd.date().toString(),
                        (RoomStateDay rsd) -> rsd.state().name()
                ));

        return roomDTO;
    }
}

我有一个将localdate(thymeleaf不喜欢它,所以我将它转换成字符串)Map到状态的Map。现在我想在一个视图中设置状态。
在我看来,我为不同的房间选择了特定的日期,在下拉菜单中,我想将选择更改为特定的状态。由于直接发回列表效果不好,我读到可以使用 Package 器类将列表发回。在控制器中,我将列表放入 Package 器,并将其作为modelattribute发送到视图。
Package 类:

@Getter
@Setter
public class RoomsRequestWrapper {

  private List<RoomDTO> rooms;

  public RoomsRequestWrapper(List<RoomDTO> rooms) {
    this.rooms = rooms;
  }

}

在控制器中:

@GetMapping(VIEW_ROOMS_URL)
    public String viewRooms(Model model){
        List<RoomDTO> rooms = roomService.getAllRooms();
        //gets first day of current week
        LocalDate now = LocalDate.now();
        TemporalField fieldISO = WeekFields.of(Locale.GERMANY).dayOfWeek();
        LocalDate firstDayOfWeek = now.with(fieldISO, 1);
        firstDayOfWeek = firstDayOfWeek.minusDays(1);
        RoomsRequestWrapper roomsWrapper = new RoomsRequestWrapper(rooms);

        model.addAttribute("title", "Zimmerplan");
        model.addAttribute("firstDayOfSelectedWeek", now.with(fieldISO, 1));
        model.addAttribute("roomsWrapper", roomsWrapper);
        model.addAttribute("isCheckIn", false);
        return VIEW_ROOMS_VIEW;
    }

当被发回时,我要么得到一个从字符串到列表的Map不起作用的错误,要么列表只是空的。显然,我的列表转换成字符串属性?
错误代码:

WARN 4172 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.ModelAttributeMethodProcessor$1: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'roomsWrapper' on field 'rooms': rejected value [[at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@42db53d3, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@4e53cb18, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@79b61c3c, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@20b3ce56, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@386e5458, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@68355346, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@212bf580, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@1fd44e8d, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@555215bb, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@173fd334, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@69c6b3a6, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@54db2835, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@11e110d8, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@782ed7a3, at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO@24a91083]]; codes [typeMismatch.roomsWrapper.rooms,typeMismatch.rooms,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [roomsWrapper.rooms,rooms]; arguments []; default message [rooms]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.util.List'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'at.fhv.team_e.Hotelverwaltung.application.dto.RoomDTO': no matching editors or conversion strategy found]]

在这一点上,我真的不确定,因为我尝试了很多变化,每次我要么得到转换错误或列表只是空的。
每天在表格单元格中显示的房间状态:

<div class="day" th:each="day: ${#numbers.sequence(0, 5)}">
                        <td colspan="2" th:classappend="${room.roomStateDays.get(firstDayOfSelectedWeek.plusDays(day).toString())}">
                            <input hidden name="state" th:field="*{roomsWrapper.rooms[__${iter.index}__].roomStateDays[__${firstDayOfSelectedWeek.toString()}__]}">
                        </td>
                    </div>

提交变更的表格:

<div class="dropdown-menu">
                    <form method="post" th:object="${roomsWrapper}" th:action="@{/rooms/updateroomplan}">
                        <input th:field="*{rooms}" th:value="${rooms}" hidden>
                        <a type="submit" class="dropdown-item" onclick="fillOutDatesToUpdate('FREE', this.parentElement)">frei</a>
                    </form>
                    <form method="post" th:object="${roomsWrapper}" th:action="@{/rooms/updateroomplan}">
                        <input th:field="*{rooms}" th:value="${rooms}" hidden>
                        <a class="dropdown-item" onclick="fillOutDatesToUpdate('BUSY', this.parentElement)">belegt</a>
                    </form>
                    <form method="post" th:object="${roomsWrapper}" th:action="@{/rooms/updateroomplan}">
                        <input th:field="*{rooms}" th:value="${rooms}" hidden>
                        <a class="dropdown-item" onclick="fillOutDatesToUpdate('BEING_CLEANED', this.parentElement)">wird geputzt</a>
                    </form>
                    <form method="post" th:object="${roomsWrapper}" th:action="@{/rooms/updateroomplan}">
                        <input th:field="*{rooms}" th:value="${rooms}" hidden>
                        <a class="dropdown-item" onclick="fillOutDatesToUpdate('MAINTENANCE', this.parentElement)">Wartung</a>
                    </form>
</div>

通过我在表格单元格中创建的隐藏输入字段,在特定日期将突出显示/选定的房间更改为所需状态(我要更改的房间)的函数:

function fillOutDatesToUpdate(stateName, parent) {
        $('.highlighted').each(function(){
            $(this).children(":first").val(stateName);
        });
        parent.submit();
    };

提交值之后,我通过model属性接收 Package 器。

@PostMapping(UPDATE_ROOMS_URL)
    public ModelAndView updateRoomStates(@ModelAttribute("roomsWrapper") RoomsRequestWrapper rooms, Model model){
        //manipulation service to be implemented
        return new ModelAndView("redirect:" + VIEW_ROOMS_URL);
    }

有没有什么方法可以提交这些更改,或者不能与那些在thymeleaf的高级课程一起工作?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题