Spring MVC 将对象从列表传递到后端点

问题描述 投票:0回答:1

我有一个弹簧控制器,其后端点接收到

WordDto
。另一个获取端点将
WordDto
列表放入
ModelMap
中并显示一个 jsp 文件。在 jsp 文件中,我在表的一行中显示列表的每个条目。每行还有一个按钮,应该使用该行的
WordDto
来调用后端点。

但我不知道,如何将

WordDto
从行正确传递到后端点。

端点

    @GetMapping("/generate")
    public String generateWords(ModelMap model, @RequestParam(value = "language", defaultValue = "-1", required = false) long language, @RequestParam(value = "amount", defaultValue = "-1", required = false) int amount)
    {
        model.put("language", language);
        model.put("amount", amount);

        LanguageData languageData = languageService.getLanguageData(language);

        model.put("languageName", languageData.getName());
        model.put("words", generationService.generateWords(language, amount));

        return "list-words";
    }

    @PostMapping("word")
    public String saveWord(ModelMap model, HttpServletRequest request, @Valid WordDto singleWord)
    {
        translationService.saveOrGetWord(singleWord);

        return "redirect:" + request.getHeader("Referer");
    }

jsp 文件

<body>
    <%--@elvariable id="languageName" type="java.lang.String"--%>
    <h1>Generated words with '${languageName}'</h1>
    <table class="table">
        <thead>
        <tr>
            <th>Word</th>
        </tr>
        </thead>
        <tbody>
        <%--@elvariable id="words" type="java.util.List"--%>
        <c:forEach items="${words}" var="singleWord">
            <tr>
                <td>
                        ${singleWord.word}
                </td>
                <td>
                    <form:form method="post" action="word" modelAttribute="singleWord.word">
                        <form:input type="hidden" path="languageId"/>
                        <form:input type="text" path="word"/>
                    </form:form>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</body>

如何将 jsp 表中连续的

WordDto
发送到
/word
端点?

spring-mvc spring-restcontroller spring-form
1个回答
0
投票

我成功了,但只是采用了不同的方法,(在我看来)这并不像我最初计划的那么好。新版本需要两个参数,而不是一次接受整个对象。

我将表格调整为如下所示:

        <tbody>
        <%--@elvariable id="words" type="java.util.List"--%>
        <c:forEach items="${words}" var="singleWord" varStatus="status">
            <tr>
                <form:form method="post" action="word">
                    <input type="hidden" name="languageId" value="${singleWord.languageId}"/>
                    <td>
                        <label>
                            <input type="text" name="word" value="${singleWord.word}"/>
                        </label>
                    </td>
                    <td>
                            <%--TODO: Add button to show translation page for this word (Should also save the word before showing the page, if it is not yet saved)--%>
                    </td>
                    <td>
                        <input type="submit" class="btn-success" value="Save">
                    </td>
                </form:form>
            </tr>
        </c:forEach>
        </tbody

端点也必须修改:

    @PostMapping("word")
    public String saveWord(HttpServletRequest request, @RequestParam(value = "languageId") long languageId, @RequestParam(value = "word") String word)
    {
        translationService.saveOrGetWord(new WordDto(languageId, word));

        return "redirect:" + request.getHeader("Referer");
    }
© www.soinside.com 2019 - 2024. All rights reserved.