数据未从Thymeleaf传递到Spring Controller

时间:2019-11-15 07:27:23

标签: spring-mvc thymeleaf

我有一个表格,里面有一张桌子。首先通过相同的对象表单绑定填充表,然后尝试将数据发送回控制器,但列表中的表单字段为空。

控制器:

@PostMapping("/searchEvent")
    public String searchEvent(@BindingResult bindingResult, Model model) {
        List<HeldUpEvent> heldUpEvents = new ArrayList<>();

        List<ReleaseSearchResult> searchResults = new ArrayList<>();

        if (!bindingResult.hasErrors()) {
            heldUpEvents = eventService.heldUpEvents(filter);
        }

        for (HeldUpEvent heldUpEvent : heldUpEvents) {
            searchResults.add(new ReleaseSearchResult(heldUpEvent));
        }
        ReleaseSearchForm form = new ReleaseSearchForm();
        form.setReleaseSearchResults(searchResults);

        model.addAttribute("releaseForm", form);

        return "searchEventResult";
    }

    @PostMapping("/releaseSearchEvent")
    public String releaseSearchEvent(@ModelAttribute("releaseForm") ReleaseSearchForm form, Model model) {
        model.asMap().get("searchResults");
        return "releaseEventResult";
    }

HTML表单:

<form id="releaseSearchForm" method="POST" th:object="${releaseForm}"
        th:action="@{/releaseSearchEvent}" action="#">

        <table
            class="table table-striped table-sm table-condensed table-hover">
            <thead>
                <tr>
                    <th>Source System Id</th>
                    <th>Event Code</th>
                    <th>Event Category</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="searchResult, i : *{releaseSearchResults}">
                    <td class="table-primary" 
                        th:field="*{releaseSearchResults[__${i.index}__].heldUpEvent.msgKey}"
                        th:text="${searchResult.heldUpEvent.msgKey}"></td>
                    <td
                        th:field="*{releaseSearchResults[__${i.index}__].heldUpEvent.eventCode}"
                        th:text="${searchResult.heldUpEvent.eventCode}">Event code
                        like DVCA etc</td>
                </tr>
            </tbody>
        </table>
        <div class="form-actions">
            <button type="submit" value="submit" class="btn btn-success">Release
                Selected Events(s).</button>
        </div>
    </form>

ReleaseForm:

public class ReleaseSearchForm {
    private List<ReleaseSearchResult> releaseSearchResults;

    public List<ReleaseSearchResult> getReleaseSearchResults() {
        return releaseSearchResults;
    }

    public void setReleaseSearchResults(
            List<ReleaseSearchResult> releaseSearchResults) {
        this.releaseSearchResults = releaseSearchResults;
    }
}

ReleaseSearchResult:

public class ReleaseSearchResult {
    private boolean selected;
    private HeldUpEvent heldUpEvent;

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public HeldUpEvent getHeldUpEvent() {
        return heldUpEvent;
    }

    public void setHeldUpEvent(HeldUpEvent heldUpEvent) {
        this.heldUpEvent = heldUpEvent;
    }

    public ReleaseSearchResult(HeldUpEvent heldUpEvent) {
        this.heldUpEvent = heldUpEvent;
    }

    public ReleaseSearchResult() {}
}

当我提交表单时,在我的控制器中我得到了ReleaseSearchForm,但是里面的列表为空。请协助。

0 个答案:

没有答案