【问题标题】:Set<T> Attribute of Object becomes null after thymeleaf post在 thymeleaf 发布后,对象的 Set<T> 属性变为空
【发布时间】:2019-12-05 20:51:40
【问题描述】:

我有一个 DTO,其中包含一组另一个 DTO。将其传递到百里香模板是没有问题的。但是,我如何从模板中检索它?

现在 set 属性始终为空

DTO:

public class Partner{
    @Id
    Long id;

    Set<Account> accounts;
}

public class Account{
    @Id
    Long id;

    String name;
}

模板:

<form method="POST" th:object="${partner}" th:action="@{/postmystuff}">
    <div th:each="acc : *{accounts}">
        <input type="text" th:field="${acc}" />
    </div>
    <button type="submit">send</button>
</form>

控制器:

@Controller
public class SomeController{
    //...
    @GetMapping("/")
    public String getMySite(Model m){
        Partner p = new Partner(accounts); //accounts is a set of 10 accounts
        m.addAttribute("partner", p);
        return "mytemplate";
    }

    @PostMapping("/postmystuff")
    public String postMyStuff(@ModelAttribute Partner p){
        System.out.println(p); //for now we just print
        return "redirect:/";
    }
}

提交后,它会打印对象的字符串表示,但所有属性都是空的。

【问题讨论】:

    标签: java spring spring-mvc thymeleaf


    【解决方案1】:

    您尚未将 Partner 对象添加到模型中,因此模板无法访问它。在您的 return 语句之前,添加以下行:

    m.addAttribute("partner", p);
    

    【讨论】:

    • 哦,我做到了。忘了在问题中添加这个,我的错。但是是的,我做到了。当到达后期映射部分时,事情就没有解决。在postmapping中,print实际上打印了一个partner对象,但是它的所有属性都是null
    猜你喜欢
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2018-06-15
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多