【问题标题】:Controller sends empty Object from Thymeleaf - Spring Boot控制器从 Thymeleaf 发送空对象 - Spring Boot
【发布时间】:2020-10-19 00:27:29
【问题描述】:

大家好!

我一直在 Spring Boot 中实现服务 允许用户向服务器发送匿名问题。

我已经实现了大多数后端,例如添加用户等,现在我一直在努力解决一个从用户那里获取答案并将其发送到服务器(保存在数据库中)的操作。 包含答案 (fillSurvey) 的对象被发送为空。在同样的逻辑中,从表单记录用户字段是正确发送的。

此端点显示问题:

@RequestMapping(path = {"/try", "/try/{id}"})
    public String tryCompletingSurvey(Model model, @PathVariable("id") Long id) {
        Connection connection = connectionService.getConnection(id);
        FilledSurvey filledSurvey = connection.getSurvey().getTemplate();

        for (FilledQuestion filledQuestion : filledSurvey.getFilledQuestions()) {
            filledQuestion.getFilledAnswers().get(0).setCheck(true);
        }

        model.addAttribute("filledSurvey", filledSurvey);

        return "completing/completing";
    }

这是 thymeleaf html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Completing survey</title>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
</head>

<body>
<center>
    <form action="#" th:action="@{/user/surveys/finish}" th:object="${filledSurvey}" method="post">

<!--        <div th:each="question, questionStat : ${survey.getFilledQuestions()}" >-->
<!--            <p th:text="${question.getQuestion()}"></p>-->
<!--            <div th:each="answer, answerStat: ${question.getFilledAnswers()}" >-->
<!--                <input type="radio"-->
<!--                       th:name="question+${questionStat.index}"-->
<!--                       th:field="*{}"-->
<!--                       th:value="${true}">-->
<!--                <label th:text="${answer.answer}">-->
<!--                </label>-->
<!--            </div>-->
<!--        </div>-->

        <h2>Survey name: </h2>
        <h3 th:text="${filledSurvey.getSurveyName()}"></h3>

        <h2>Number of questions: </h2>
        <h3 th:text="${filledSurvey.filledQuestions.size()}"></h3>
        <div class="col-md-6">
            <input type="submit" style="align-content: center" class="btn btn-primary" value="  Send  ">
        </div>
    </form>
</center>
</body>
</html>

这是存储来自 thymeleaf 的空对象的端点:

@RequestMapping(path = "/finish", method = RequestMethod.POST)
    public String getHash(FilledSurvey filledSurvey) {

        StringBuilder sb = new StringBuilder();
        for (FilledQuestion question : filledSurvey.getFilledQuestions()) {
            for (FilledAnswer answer : question.getFilledAnswers()) {
                if (answer.isCheck()) sb.append(answer.getAnswer());
            }
        }
        LocalDateTime date = LocalDateTime.now();
        sb.append(date);

        String hash = sb.toString();
        hash = Base64.getEncoder().encodeToString(sb.toString().getBytes());
        filledSurvey.setHash(hash);

        surveyMagazinService.addSurveyToMagazin(filledSurvey);

        return "completing/finish";
    }

我现在将代码更改为自动标记答案。 这是下一个端点中对象的图片: filledSurvey object

我知道这是一个常见问题,但我一直在寻找答案一段时间,但无法弄清楚。我在控制台中也没有错误。如有任何帮助或反馈,我将不胜感激。

【问题讨论】:

    标签: java spring spring-boot model-view-controller thymeleaf


    【解决方案1】:

    如果我理解正确,我会看到以下问题:
    您正在使用表单提交调查数据并使用 th:object="${fillSurvey}" 绑定数据。但实际上在提交表单时没有将数据发送回控制器,因为没有定义应用了 th:field 属性的输入字段。
    将在提交时发送到服务器的请求将包含您分配 th:field 属性的所有字段的表单编码数据。控制器将在 getHash 方法中使用 java bean 约定将表单编码数据映射到 FilledSurvey 对象。
    编辑:您可以尝试添加 @ModelAttribute 注释吗:

    @RequestMapping(path = "/finish", method = RequestMethod.POST)
        public String getHash(@ModelAttribute FilledSurvey filledSurvey) {
    ...
    

    尝试在表单中添加这样的输入字段:

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

    这应该至少为您提供一个 FilledSurvey 对象,该对象的 id 设置在您的“/finish”端点上。然后,您可以使用 id 来获取调查,就像在第一个代码 sn-p 中所做的那样。

    您在问题列表中使用 th:field 的方式将不起作用,因为 spring 无法映射这种结构。请参阅https://spring.io/guides/gs/handling-form-submission/ 以了解表单提交如何与 spring mvc 一起使用。
    我希望这会有所帮助,最好的问候;)

    【讨论】:

    • 这个方法我已经试过了。对象仍然是空的。
    • @Dejvinczi 请查看我的编辑。可以尝试添加@ModelAttribute 注解吗?
    • 我也试过这种方式。我可能搜索了整个堆栈,这个方法也没有做任何事情..
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 2015-09-04
    • 2018-03-10
    • 2019-09-29
    • 2016-08-05
    • 2021-08-01
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多